Advertisement
zenetico

Addition/Multiplication Quiz Generator Python

Apr 8th, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. def perform_test():
  2. '''The function begins with a welcome message. Then prompts the user to
  3. input integers for two things:
  4. 1) What operation the user would like to be tested on
  5. (0 for addition and 1 for multiplcation)
  6. 2) The number of questions they would like to receive
  7.  
  8. The test will then produce n questions in the chosen operation.
  9. If the user answers a question correctly, proceed to the next question.
  10. If the question is answered incorrectly, inform the user that it is
  11. incorrect and tell them the correct answer.
  12.  
  13. After the test, the results are printed as follows:
  14. If the final score is >=80%: Excellent work!
  15. If final score is 60 <= g <= 80: Not bad but you should study more.
  16. If final score is < 60: You need to practice more'''
  17.  
  18. import random
  19. print('Welcome to the Addition/Multiplication Test!')
  20. print('Which operation would you like to be tested on?')
  21. #Prompts user to select operation and number of questions
  22. test_type = int(input('Input 0 for addition and 1 for multiplication: '))
  23.  
  24. #ADDITION TEST
  25. if test_type == 0:
  26. n=int(input('Enter the number of questions for the test: '))
  27. if n==0:
  28. print('Goodbye')
  29. exit()
  30. #Created a total_q variable to serve as a calculation of the
  31. #overall grade as n will be subtracted by 1 with each new question
  32. #'correct' is a variable that will increase by 1 with each right answer
  33. total_q = n
  34. correct = 0
  35.  
  36. #This part will repeatedly display questions until n=0
  37. while n > 0:
  38. a=random.randint(0,9)
  39. b=random.randint(0,9)
  40. n=n-1
  41. s=a+b
  42. print(a,'+',b)
  43. q=int(input('What is the answer?: '))
  44. if q == s:
  45. correct = correct + 1
  46. else:
  47. print('Incorrect. The answer is',s,'.')
  48.  
  49. if n == 0:
  50. #grade is the score received after taking the test
  51. grade = (correct / total_q)*100
  52. if grade >= 80:
  53. print('Your grade is',grade,'. Nice work!')
  54. elif 60 <= grade < 80:
  55. print('Your grade is',grade,'. Not bad but a little more studying would help.')
  56. elif grade < 60:
  57. print('Your grade is',grade,'. You need to study more. Ask your teacher for help.')
  58.  
  59. #MULTIPLICATION TEST
  60. if test_type == 1:
  61. n=int(input('Enter the number of questions for the test: '))
  62. if n==0:
  63. print('Goodbye')
  64. exit()
  65. #Created a total_q variable to serve as a calculation of the
  66. #overall grade as n will be subtracted by 1 with each new question
  67. #'correct' is a variable that will increase by 1 with each right answer
  68. total_q = n
  69. correct = 0
  70.  
  71. #This part will repeatedly display questions until n=0
  72. while n > 0:
  73. a=random.randint(0,9)
  74. b=random.randint(0,9)
  75. n=n-1
  76. p=a*b
  77. print(a,'*',b)
  78. q=int(input('What is the answer?: '))
  79. if q == p:
  80. correct = correct + 1
  81. else:
  82. print('Incorrect. The answer is',p,'.')
  83.  
  84. if n == 0:
  85. #grade is the score received after taking the test
  86. grade = (correct / total_q)*100
  87. if grade >= 80:
  88. print('Your grade is',grade,'. Nice work!')
  89. elif 60 <= grade < 80:
  90. print('Your grade is',grade,'. Not bad but a little more studying would help.')
  91. elif grade < 60:
  92. print('Your grade is',grade,'. You need to study more. Ask your teacher for help.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement