Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Importing Libraries
- import time
- def mainMenu():
- #Main Menu
- print("Knowledge Quiz")
- print("\n1) Register\n2) Login\n3) Console\n4) Quit")
- #User Input
- menuCH = int(input("O>"))
- #Simple Selection
- if menuCH == 1:
- register()
- elif menuCH == 2:
- login()
- elif menuCH == 3:
- console()
- elif menuCH == 4:
- print("Exiting...")
- time.sleep(1)
- quit()
- #Error handling
- else:
- print("Invalid Input!!!")
- time.sleep(1)
- print("\n\n\n\n\n\n")
- mainMenu()
- def register():
- # Asking for user input to be saved as their account details
- f = open("User Data\data.txt",'a+')
- forenameI = input("Forename: ")
- surnameI = input("Surname: ")
- passwordI = input("Password: ")
- age = input("Age: ")
- username = forenameI[0:3]+age
- # Saving the register data to a file
- f.write("\n")
- f.write(username)
- f.write("\n")
- f.write(forenameI)
- f.write("\n")
- f.write(surnameI)
- f.write("\n")
- f.write(passwordI)
- f.write("\n")
- f.write(age)
- f.write("\n")
- #User Friendly Information print out
- print("Your username is ",username," , you will need it to log in.\n")
- print("Register successful!")
- f.close()
- input("Press Any Key to continue...")
- mainMenu()
- def login():
- #Simple Log In algorithm which looks through a file for a matching username and password
- f = open("User Data\data.txt",'r+')
- search = f.readlines()
- usernameL = input("Username: ")
- for x in range(len(search)):
- if search[x].replace("\n","") == usernameL:
- passwordL = input("Password: ")
- if passwordL == search[x+3].replace("\n",""):
- print("Logging in...")
- time.sleep(1)
- quizMenu()
- else:
- print("Invalid password!")
- print("Try again.")
- time.sleep(0.5)
- login()
- #Error Handling
- print("Username not found!")
- login()
- def quizMenu():
- #Simple Selection
- print("\n\nSelect the topic")
- print("\n1) History\n2) Music\n3) Computer Science\n4) Return to Main Menu")
- quizCH = int(input("\nO>"))
- if quizCH == 1:
- print("history")
- history()
- elif quizCH == 2:
- print("music")
- #music()
- elif quizCH == 3:
- print("computer science")
- #cs()
- elif quizCH == 4:
- mainMenu()
- def history():
- # Opening various files
- q = open("User Data\Q&A\HistoryQ.txt",'r+')
- ah = open("User Data\Q&A\HistoryAH.txt",'r+')
- am = open("User Data\Q&A\HistoryAM.txt",'r+')
- ae = open("User Data\Q&A\HistoryAE.txt",'r+')
- #Reading the files into lists
- questions = q.readlines()
- answersh = ah.readlines()
- answersm = am.readlines()
- answerse = ae.readlines()
- score = 0
- print("Choose your difficulty:\n\n1) Easy\n2) Medium\n3) Hard")
- diff = int(input("O> "))
- #Confusing attempt at difficulty level handing, definitely could be improved
- if diff == 1:
- for x in range(5):
- answere = answerse[x].split(",")
- print(x+1,".) ",questions[x])
- print("A) ",answere[0])
- print("B) ",answere[1])
- userAnswer = input("Answer: ")
- if userAnswer.upper() == answere[2].replace("\n",""):
- score = score + 1
- print("You have answered ",((score*2)/10)*100,"% of questions correctly!")
- print("You have earned ",score," points!")
- elif diff == 2:
- for x in range(5):
- answerm = answersm[x].split(",")
- print(x+1,".) ",questions[x])
- print("A) ",answerm[0])
- print("B) ",answerm[1])
- print("C) ",answerm[2])
- userAnswer = input("Answer: ")
- if userAnswer.upper() == answerm[3].replace("\n",""):
- score = score + 1
- print("You have answered ",((score*2)/10)*100,"% of questions correctly!")
- print("You have earned ",score," points!")
- elif diff == 3:
- for x in range(5):
- answerh = answersh[x].split(",")
- print(x+1,".) ",questions[x])
- print("A) ",answerh[0])
- print("B) ",answerh[1])
- print("C) ",answerh[2])
- print("D) ",answerh[3])
- userAnswer = input("Answer: ")
- if userAnswer.upper() == answerh[4].replace("\n",""):
- score = score + 1
- print("You have answered ",((score*2)/10)*100,"% of questions correctly!")
- print("You have earned ",score," points!")
- else:
- print("Wrong input.")
- time.sleep(1)
- history()
- # Program Init
- mainMenu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement