Advertisement
ssoni

mathtestsoni.py

Dec 7th, 2023
757
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.09 KB | None | 0 0
  1. import random
  2.  
  3. def getOp():
  4.     op = random.randrange(1,5)
  5.  
  6.     match op:
  7.         case 1:
  8.             return '+'
  9.         case 2:
  10.             return '-'
  11.         case 3:
  12.             return '*'
  13.         case 4:
  14.             return '/'
  15.  
  16. def solve(n1,n2,op):
  17.     if op == '+':
  18.         return n1+n2
  19.     elif op == '-':
  20.         return n1-n2
  21.     elif op == '*':
  22.         return n1*n2
  23.     elif op == '/':
  24.         return round(n1/n2, 1)
  25.  
  26. def askQuestion(q,s):
  27.     ans = float(input(q))
  28.     if ans == s:
  29.         return True
  30.     else:
  31.         return False
  32.  
  33. def main():
  34.     numWrong = 0
  35.     numCorrect = 0
  36.     while (numWrong < 3):
  37.         a = random.randrange(1,10)
  38.         b = random.randrange(1,10)
  39.         oper = getOp()
  40.  
  41.         solution = solve(a,b,oper)
  42.         question = f'{a} {oper} {b} = '
  43.         result = askQuestion(question,solution)
  44.  
  45.         if result == True:
  46.             numCorrect = numCorrect + 1
  47.             print('Correct')
  48.         else:
  49.             print('Incorrect')
  50.             numWrong = numWrong + 1
  51.  
  52.     print(f'You got {numCorrect} correct')
  53. main()
  54.  
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement