Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. from random import randint
  2.  
  3. def display():
  4. print("Welcome to my awesome Math game")
  5. print("What is...")
  6.  
  7. def getAnswer():
  8. x = randint(1,10)
  9. y = randint(1,10)
  10. op = ['+', '-', '*']
  11. op_int = randint(0,2)
  12.  
  13. print(x, op[op_int], y, '=')
  14.  
  15. ans = 0
  16. if op_int == 0:
  17. ans = x + y
  18. elif op_int == 1:
  19. ans = x - y
  20. else:
  21. ans = x * y
  22. return ans
  23.  
  24. def getUserInput():
  25. user_ans = input("The answer is: ")
  26. user_ans = int(user_ans)
  27. return user_ans
  28.  
  29. def getResult(computer_answer, user_answer):
  30. if computer_answer == user_answer:
  31. print("You are correct!")
  32. return 1
  33. else:
  34. print("You are wrong.The answer is", computer_answer,".")
  35. return 0
  36.  
  37. score = 0
  38. repeat = True
  39. display()
  40. while repeat == True:
  41. ca = getAnswer()
  42. #print(ca)
  43. ua = getUserInput()
  44. #print(ua)
  45. x = getResult(ca,ua)
  46. print()
  47. score = score + x
  48. print('Score:', score)
  49. print()
  50. r = input("Wanna go again?(y/n)")
  51. if r == 'n':
  52. repeat = False
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement