Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pyinputplus as pyip
- import random, time
- numberOfQuestions = 10
- correctAnswers = 0
- for questionNumber in range(numberOfQuestions):
- #pick 2 random numbers
- num1 = random.randint(0,9)
- num2 = random.randint(0,9)
- prompt = '#%s: %s x %s = ' % (questionNumber + 1, num1, num2)
- try:
- pyip.inputStr(
- prompt,
- allowRegexes = ['^%s$' % (num1 * num2)],
- blockRegexes = [('.*', 'Incorrect!')],
- timeout = 8,
- limit = 3
- )
- except pyip.TimeoutException:
- print('Out of time!')
- except pyip.RetryLimitException:
- print('Out of tries!')
- else:
- print('Correct!')
- correctAnswers += 1
- time.sleep(1) #Brief pause to let user see the result.
- print('Score: %s / %s' % (CorrectAnswers, numberOfQuestions))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement