Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from random import randint
- from random import seed
- import math
- import base64
- import funcs
- import sys
- import os
- print("--- MATHS QUIZ ---")
- # VARIABLES #
- FullCorrect = 0
- FullWrong = 0
- wins = 0
- username = ""
- add = True
- sub = True
- mul = True
- div = True
- _pow = True
- _contain = False
- start = False
- password = ""
- ###
- try:
- os.mkdir("saves")
- except FileExistsError:
- pass
- # FUNCTIONS #
- def Save():
- 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)))
- funcs.Write("saves\%s.txt" % (username), save)
- funcs.Base64Encode("saves\%s.txt" % username)
- def Login():
- global password
- global username
- username = input("Please enter your username.\n>")
- try:
- access = False
- while not access:
- store = funcs.Base64Read("saves\%s.txt" % username)
- try:
- exec(store)
- passattempt = input("Enter the password for %s \n>" % username)
- if password != passattempt:
- print("Invalid password.")
- else:
- access = True
- except FileNotFoundError:
- raise
- except:
- print("Save file corrupt! Deleting...")
- os.remove("saves\%s.txt" % username)
- print("------------------------------\n")
- Login()
- except FileNotFoundError:
- print("No save file found. Creating one..")
- _loginpass = "a"
- _loginpass_ = "b"
- while _loginpass != _loginpass_:
- _loginpass = input("Please type the password you want for this account \n>")
- _loginpass_ = input("Please confirm the password \n>")
- if _loginpass == _loginpass_:
- password = _loginpass
- Save()
- else:
- print("Passwords did not match!\n")
- def CheckInt(string):
- try:
- result = int(string)
- return True
- except ValueError:
- return False
- def CheckFloat(string):
- try:
- result = float(string)
- return True
- except ValueError:
- return False
- ###
- # GAME #
- def game():
- global _TEMPSAVE_
- print("At any time during the quiz, type \"stop\" to end the quiz. Don't worry, your stats save!")
- lives = 5
- Q = 0
- Correct = 0
- while Correct < 10 and lives > 0:
- global mul
- global add
- global div
- global sub
- global _pow
- funcs = ["+", "-", "*", "^", "/"]
- funcsInt = ["bigger+lower", "bigger-lower", "bigger*lower", "int(math.pow(bigger, lower))", "bigger/lower"]
- qMax1 = [500, 500, 15, 10, 15]
- qMax2 = [500, 499, 15, 3, 15]
- qTypeMax = 4
- qTypes = [add, sub, mul, _pow, div]
- for x in range(4, -1, -1):
- if not qTypes[x]:
- qTypeMax -= 1
- del qMax1[x]
- del qMax2[x]
- del funcsInt[x]
- del funcs[x]
- if len(funcs) == 0:
- print("Can not start with no questions! Returning to command line.")
- return
- qType = randint(0, qTypeMax)
- '''
- 0 = Addition
- 1 = Subtraction
- 2 = Multiplication
- 3 = Power
- 4 = Division
- '''
- int1 = randint(1, qMax1[qType])
- int2 = randint(1, qMax2[qType])
- if qType==4:
- int2 = int2*(int1/2)
- if int1 > int2:
- bigger = int1
- lower = int2
- else:
- bigger = int2
- lower = int1
- print("Question " + str(Q + 1) + ":")
- guess = input("What is " + str(bigger) + funcs[qType] + str(lower) + "?\n>")
- answer = float(eval(funcsInt[qType]))
- tests = [CheckInt(guess), CheckFloat(guess)]
- if guess == "stop":
- print("Do not end the program yet, saving...")
- Save()
- print("Saved!")
- return
- if any(tests):
- guess = float(guess)
- if answer == guess:
- global FullCorrect
- FullCorrect += 1
- Correct += 1
- Q += 1
- Save()
- print("Correct!")
- else:
- global FullWrong
- lives -= 1
- Q += 1
- FullWrong += 1
- Save()
- if lives == 1:
- print("Incorrect. " + str(lives) + " life remaining")
- else:
- print("Incorrect. " + str(lives) + " lives remaining")
- if lives < 1:
- global wins
- print("Game over, you ran out of lives!\n")
- Save()
- elif Correct == 10:
- print("Congratulations, you won!\n")
- wins += 1
- Save()
- ###
- # COMMANDS #
- boolean = ["off", "on"]
- _help = '''print("info - brings up this menu")
- print("status - displays all active questions")
- print("add - toggles addition questions")
- print("sub - toggles subtraction questions")
- print("mul - toggles multiplication questions")
- print("div - toggles division questions")
- print("pow - toggles power questions")
- print("start - starts a quiz")
- print("stats - view your alltime stats")
- print("logout - Log in under a new name")
- print("exit - Exits the program")
- print("reset - Resets save data")'''
- _status = '''stats = {
- "Addition": add,
- "Subtraction": sub,
- "Division": div,
- "Multiplication": mul,
- "Power": _pow}
- for x,y in stats.items():
- print(str(x) + " questions are " + boolean[int(y)] + ".")
- '''
- _stats = '''print("Total questions: " + str(FullCorrect + FullWrong))
- print("Total questions correct: " + str(FullCorrect))
- print("Total questions incorrect: " + str(FullWrong))
- if FullCorrect + FullWrong > 0:
- print("Percentage of questions correct: " + str(100*(FullCorrect / (FullCorrect+FullWrong))) + "%")
- else:
- print("Percentage of questions correct: N/A%")'''
- _reset = '''yn = input("Are you sure you want to reset your save data for " + username + "? All data will be lost! (Y/N) >")
- _yn = yn.lower()
- if _yn != "y" and _yn != "n":
- print("No valid input")
- elif _yn == "n":
- print("Operation cancelled.")
- else:
- print("Clearing save file..")
- os.remove("saves\%s.txt" % (username))
- print("File cleared!")'''
- commands = {
- "info": _help,
- "add": '''add = not add
- print("Addition is "+boolean[int(add)])''',
- "sub": '''sub = not sub
- print("Subtraction set "+boolean[int(sub)])''',
- "div": '''div = not div
- print("Division set "+boolean[int(div)])''',
- "mul": '''mul = not mul
- print("Multiplication set "+boolean[int(mul)])''',
- "pow": '''_pow = not _pow
- print("Powers set "+boolean[int(_pow)])''',
- "start": '''print("Starting.")
- game()''',
- "status": _status,
- "logout": "Login()",
- "stats": _stats,
- "exit": "sys.exit()",
- "reset": _reset}
- ###
- # ADMIN COMMANDS #
- # SCRIPT #
- Login()
- print("Welcome to the maths quiz! Type info for commands.")
- while True:
- inp = input("\n>")
- if inp in commands:
- exec(commands[inp])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement