Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1.  
  2. from random import choice, randint
  3.  
  4. COUNTER = 0
  5. LEVEL = 0
  6.  
  7.  
  8. def check(Num):
  9.  
  10. global COUNTER
  11. global LEVEL
  12.  
  13. while True:
  14. try:
  15. value = int(input("Please enter your Answer: "))
  16. except ValueError:
  17. print("Sorry I dont understand that")
  18. continue
  19. else:
  20. break
  21. if Num == value:
  22. print("Well Done")
  23. COUNTER += 1
  24. else:
  25. print("You are Wrong it should be : ", Num)
  26. COUNTER -= 1
  27.  
  28. LEVEL = COUNTER // 3
  29. LEVEL = max(0, min(5, LEVEL)) # restrict to 0 <= LEVEL <= 5
  30. while True:
  31. try:
  32. c = input("Do you wish to continue? Y/N: ")
  33. if c.upper() == 'Y':
  34. return 1
  35. elif c == "N":
  36. return setUp()
  37. except ValueError:
  38.  
  39. print("Try again")
  40. continue
  41.  
  42. def setUp():
  43. global LEVEL
  44.  
  45. print("Select one of the following options: ")
  46. while True:
  47. try:
  48. key = int(input("(1) Addition (2) Subtraction (3) Multiplication \n"
  49. + "(4) Division (5) Random sums (6) Quit\n"))
  50. break
  51. except ValueError:
  52. print('Bad input!')
  53. continue
  54.  
  55. while True:
  56. select = key
  57.  
  58.  
  59. if key == 5:
  60. select = randint(1, 4)
  61.  
  62. num1 = randint(1, 4 + LEVEL)
  63. num2 = randint(1, 4 + LEVEL)
  64.  
  65. dic = {1: num1 + num2, 2: num1 - num2, 3: num1 * num2, 4: None, 5: None}
  66. support = {1: "+", 2: "-", 3: "*", 4: "/"}
  67.  
  68.  
  69.  
  70. if select != 4 and select != 2 and select!=6:
  71. print("what is the answer for : {} {} {}".format(num1, support[select], num2))
  72.  
  73. if select == 6:
  74. print("Have a good day")
  75. exit()
  76.  
  77. elif select not in dic:
  78. print("Invalid choice.")
  79. break
  80. elif 1 <= select <= 3 or select == 5:
  81.  
  82. if select == 2:
  83. num3 = randint(1, num1)
  84. print("what is the answer for : {} {} {}".format(num1, "-", num3))
  85. res = check(num1 - num3)
  86.  
  87. else:
  88. res = check(dic[select])
  89. else:
  90. div = [element for element in range(1, num1 + 1) if num1 % element == 0]
  91. num2 = choice(div)
  92. print(f'What is the answer for: {num1} / {num2}')
  93. res = check(num1 / num2)
  94.  
  95. if res == 0: #
  96. break # we quit the while True
  97. # back to the top of while True
  98.  
  99.  
  100. setUp()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement