Advertisement
Guest User

Maths

a guest
Jan 24th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1. from random import randint
  2. from random import seed
  3. import math
  4. import base64
  5. import funcs
  6. import sys
  7. import os
  8. print("--- MATHS QUIZ ---")
  9.  
  10. # VARIABLES #
  11. FullCorrect = 0
  12. FullWrong = 0
  13. wins = 0
  14. username = ""
  15. add = True
  16. sub = True
  17. mul = True
  18. div = True
  19. _pow = True
  20. _contain = False
  21. start = False
  22. password = ""
  23. ###
  24.  
  25. try:
  26. os.mkdir("saves")
  27. except FileExistsError:
  28. pass
  29.  
  30. # FUNCTIONS #
  31. def Save():
  32. save = str("global wins\r\nglobal FullCorrect\r\nglobal FullWrong\r\nglobal password\r\nwins = %s\r\npassword = \"%s\"\r\nFullCorrect = %s\r\nFullWrong = %s\r\n" % (str(wins), str(password), str(FullCorrect), str(FullWrong)))
  33. funcs.Write("saves\%s.txt" % (username), save)
  34. funcs.Base64Encode("saves\%s.txt" % username)
  35. def Login():
  36. global password
  37. global username
  38. username = input("Please enter your username.\n>")
  39. try:
  40. access = False
  41. while not access:
  42. store = funcs.Base64Read("saves\%s.txt" % username)
  43. try:
  44. exec(store)
  45. passattempt = input("Enter the password for %s \n>" % username)
  46. if password != passattempt:
  47. print("Invalid password.")
  48. else:
  49. access = True
  50. except FileNotFoundError:
  51. raise
  52. except:
  53. print("Save file corrupt! Deleting...")
  54. os.remove("saves\%s.txt" % username)
  55. print("------------------------------\n")
  56. Login()
  57. except FileNotFoundError:
  58. print("No save file found. Creating one..")
  59. _loginpass = "a"
  60. _loginpass_ = "b"
  61. while _loginpass != _loginpass_:
  62. _loginpass = input("Please type the password you want for this account \n>")
  63. _loginpass_ = input("Please confirm the password \n>")
  64. if _loginpass == _loginpass_:
  65. password = _loginpass
  66. Save()
  67. else:
  68. print("Passwords did not match!\n")
  69. def CheckInt(string):
  70. try:
  71. result = int(string)
  72. return True
  73. except ValueError:
  74. return False
  75.  
  76. def CheckFloat(string):
  77. try:
  78. result = float(string)
  79. return True
  80. except ValueError:
  81. return False
  82. ###
  83.  
  84. # GAME #
  85.  
  86. def game():
  87. global _TEMPSAVE_
  88. print("At any time during the quiz, type \"stop\" to end the quiz. Don't worry, your stats save!")
  89. lives = 5
  90. Q = 0
  91. Correct = 0
  92. while Correct < 10 and lives > 0:
  93. global mul
  94. global add
  95. global div
  96. global sub
  97. global _pow
  98. funcs = ["+", "-", "*", "^", "/"]
  99. funcsInt = ["bigger+lower", "bigger-lower", "bigger*lower", "int(math.pow(bigger, lower))", "bigger/lower"]
  100. qMax1 = [500, 500, 15, 10, 15]
  101. qMax2 = [500, 499, 15, 3, 15]
  102. qTypeMax = 4
  103. qTypes = [add, sub, mul, _pow, div]
  104. for x in range(4, -1, -1):
  105. if not qTypes[x]:
  106. qTypeMax -= 1
  107. del qMax1[x]
  108. del qMax2[x]
  109. del funcsInt[x]
  110. del funcs[x]
  111. if len(funcs) == 0:
  112. print("Can not start with no questions! Returning to command line.")
  113. return
  114. qType = randint(0, qTypeMax)
  115. '''
  116. 0 = Addition
  117. 1 = Subtraction
  118. 2 = Multiplication
  119. 3 = Power
  120. 4 = Division
  121. '''
  122.  
  123.  
  124.  
  125. int1 = randint(1, qMax1[qType])
  126. int2 = randint(1, qMax2[qType])
  127. if qType==4:
  128. int2 = int2*(int1/2)
  129. if int1 > int2:
  130. bigger = int1
  131. lower = int2
  132. else:
  133. bigger = int2
  134. lower = int1
  135.  
  136. print("Question " + str(Q + 1) + ":")
  137. guess = input("What is " + str(bigger) + funcs[qType] + str(lower) + "?\n>")
  138. answer = float(eval(funcsInt[qType]))
  139. tests = [CheckInt(guess), CheckFloat(guess)]
  140. if guess == "stop":
  141. print("Do not end the program yet, saving...")
  142. Save()
  143. print("Saved!")
  144. return
  145. if any(tests):
  146. guess = float(guess)
  147. if answer == guess:
  148. global FullCorrect
  149. FullCorrect += 1
  150. Correct += 1
  151. Q += 1
  152. Save()
  153. print("Correct!")
  154. else:
  155. global FullWrong
  156. lives -= 1
  157. Q += 1
  158. FullWrong += 1
  159. Save()
  160. if lives == 1:
  161. print("Incorrect. " + str(lives) + " life remaining")
  162. else:
  163. print("Incorrect. " + str(lives) + " lives remaining")
  164. if lives < 1:
  165. global wins
  166. print("Game over, you ran out of lives!\n")
  167. Save()
  168. elif Correct == 10:
  169. print("Congratulations, you won!\n")
  170. wins += 1
  171. Save()
  172. ###
  173.  
  174. # COMMANDS #
  175.  
  176. boolean = ["off", "on"]
  177. _help = '''print("info - brings up this menu")
  178. print("status - displays all active questions")
  179. print("add - toggles addition questions")
  180. print("sub - toggles subtraction questions")
  181. print("mul - toggles multiplication questions")
  182. print("div - toggles division questions")
  183. print("pow - toggles power questions")
  184. print("start - starts a quiz")
  185. print("stats - view your alltime stats")
  186. print("logout - Log in under a new name")
  187. print("exit - Exits the program")
  188. print("reset - Resets save data")'''
  189.  
  190. _status = '''stats = {
  191. "Addition": add,
  192. "Subtraction": sub,
  193. "Division": div,
  194. "Multiplication": mul,
  195. "Power": _pow}
  196.  
  197. for x,y in stats.items():
  198. print(str(x) + " questions are " + boolean[int(y)] + ".")
  199. '''
  200.  
  201. _stats = '''print("Total questions: " + str(FullCorrect + FullWrong))
  202. print("Total questions correct: " + str(FullCorrect))
  203. print("Total questions incorrect: " + str(FullWrong))
  204. if FullCorrect + FullWrong > 0:
  205. print("Percentage of questions correct: " + str(100*(FullCorrect / (FullCorrect+FullWrong))) + "%")
  206. else:
  207. print("Percentage of questions correct: N/A%")'''
  208.  
  209. _reset = '''yn = input("Are you sure you want to reset your save data for " + username + "? All data will be lost! (Y/N) >")
  210. _yn = yn.lower()
  211. if _yn != "y" and _yn != "n":
  212. print("No valid input")
  213. elif _yn == "n":
  214. print("Operation cancelled.")
  215. else:
  216. print("Clearing save file..")
  217. os.remove("saves\%s.txt" % (username))
  218. print("File cleared!")'''
  219.  
  220. commands = {
  221. "info": _help,
  222. "add": '''add = not add
  223. print("Addition is "+boolean[int(add)])''',
  224. "sub": '''sub = not sub
  225. print("Subtraction set "+boolean[int(sub)])''',
  226. "div": '''div = not div
  227. print("Division set "+boolean[int(div)])''',
  228. "mul": '''mul = not mul
  229. print("Multiplication set "+boolean[int(mul)])''',
  230. "pow": '''_pow = not _pow
  231. print("Powers set "+boolean[int(_pow)])''',
  232. "start": '''print("Starting.")
  233. game()''',
  234. "status": _status,
  235. "logout": "Login()",
  236. "stats": _stats,
  237. "exit": "sys.exit()",
  238. "reset": _reset}
  239. ###
  240.  
  241. # ADMIN COMMANDS #
  242.  
  243.  
  244.  
  245. # SCRIPT #
  246. Login()
  247. print("Welcome to the maths quiz! Type info for commands.")
  248. while True:
  249. inp = input("\n>")
  250. if inp in commands:
  251. exec(commands[inp])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement