Advertisement
Guest User

multiplicationQuiz.py

a guest
Jan 19th, 2020
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. import pyinputplus as pyip
  2. import random, time
  3.  
  4. numberOfQuestions = 10
  5. correctAnswers = 0
  6.  
  7. for questionNumber in range(numberOfQuestions):
  8.     #pick 2 random numbers
  9.     num1 = random.randint(0,9)
  10.     num2 = random.randint(0,9)
  11.    
  12.     prompt = '#%s: %s x %s = ' % (questionNumber + 1, num1, num2)
  13.    
  14.     try:
  15.         pyip.inputStr(
  16.         prompt,
  17.         allowRegexes = ['^%s$' % (num1 * num2)],
  18.         blockRegexes = [('.*', 'Incorrect!')],
  19.         timeout = 8,
  20.         limit = 3
  21.         )
  22.    
  23.     except pyip.TimeoutException:
  24.         print('Out of time!')
  25.    
  26.     except pyip.RetryLimitException:
  27.         print('Out of tries!')
  28.    
  29.     else:
  30.         print('Correct!')
  31.         correctAnswers += 1
  32.        
  33.     time.sleep(1) #Brief pause to let user see the result.
  34.  
  35. print('Score: %s / %s' % (CorrectAnswers, numberOfQuestions))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement