Advertisement
Guest User

Programming

a guest
Sep 17th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.81 KB | None | 0 0
  1. # Importing Libraries
  2. import time
  3.  
  4. def mainMenu():
  5.  
  6.     #Main Menu
  7.     print("Knowledge Quiz")
  8.     print("\n1) Register\n2) Login\n3) Console\n4) Quit")
  9.  
  10.     #User Input
  11.     menuCH = int(input("O>"))
  12.  
  13.     #Simple Selection
  14.     if menuCH == 1:
  15.         register()
  16.     elif menuCH == 2:
  17.         login()
  18.     elif menuCH == 3:
  19.         console()
  20.     elif menuCH == 4:
  21.         print("Exiting...")
  22.         time.sleep(1)
  23.         quit()
  24.  
  25.     #Error handling
  26.     else:
  27.         print("Invalid Input!!!")
  28.         time.sleep(1)
  29.         print("\n\n\n\n\n\n")
  30.         mainMenu()
  31.  
  32. def register():
  33.  
  34.     # Asking for user input to be saved as their account details
  35.     f = open("User Data\data.txt",'a+')
  36.     forenameI = input("Forename: ")
  37.     surnameI = input("Surname: ")
  38.     passwordI = input("Password: ")
  39.     age = input("Age: ")
  40.     username = forenameI[0:3]+age
  41.  
  42.     # Saving the register data to a file
  43.     f.write("\n")
  44.     f.write(username)
  45.     f.write("\n")
  46.     f.write(forenameI)
  47.     f.write("\n")
  48.     f.write(surnameI)
  49.     f.write("\n")
  50.     f.write(passwordI)
  51.     f.write("\n")
  52.     f.write(age)
  53.     f.write("\n")
  54.  
  55.     #User Friendly Information print out
  56.     print("Your username is ",username," , you will need it to log in.\n")
  57.     print("Register successful!")
  58.     f.close()
  59.     input("Press Any Key to continue...")
  60.     mainMenu()
  61.  
  62. def login():
  63.  
  64.     #Simple Log In algorithm which looks through a file for a matching username and password
  65.     f = open("User Data\data.txt",'r+')    
  66.     search = f.readlines()
  67.  
  68.     usernameL = input("Username: ")
  69.     for x in range(len(search)):
  70.         if search[x].replace("\n","") == usernameL:
  71.             passwordL = input("Password: ")
  72.             if passwordL == search[x+3].replace("\n",""):
  73.                 print("Logging in...")
  74.                 time.sleep(1)
  75.                 quizMenu()
  76.  
  77.             else:
  78.                 print("Invalid password!")
  79.                 print("Try again.")
  80.                 time.sleep(0.5)
  81.                 login()
  82.  
  83.     #Error Handling
  84.     print("Username not found!")
  85.     login()
  86.  
  87. def quizMenu():
  88.     #Simple Selection
  89.     print("\n\nSelect the topic")
  90.     print("\n1) History\n2) Music\n3) Computer Science\n4) Return to Main Menu")
  91.     quizCH = int(input("\nO>"))
  92.     if quizCH == 1:
  93.         print("history")
  94.         history()
  95.     elif quizCH == 2:
  96.         print("music")
  97.         #music()
  98.     elif quizCH == 3:
  99.         print("computer science")
  100.         #cs()
  101.     elif quizCH == 4:
  102.         mainMenu()
  103.  
  104. def history():
  105.  
  106.     # Opening various files
  107.     q = open("User Data\Q&A\HistoryQ.txt",'r+')
  108.     ah = open("User Data\Q&A\HistoryAH.txt",'r+')
  109.     am = open("User Data\Q&A\HistoryAM.txt",'r+')
  110.     ae = open("User Data\Q&A\HistoryAE.txt",'r+')
  111.  
  112.     #Reading the files into lists
  113.     questions = q.readlines()
  114.     answersh = ah.readlines()
  115.     answersm = am.readlines()
  116.     answerse = ae.readlines()
  117.  
  118.     score = 0
  119.  
  120.     print("Choose your difficulty:\n\n1) Easy\n2) Medium\n3) Hard")
  121.     diff = int(input("O> "))
  122.  
  123.     #Confusing attempt at difficulty level handing, definitely could be improved
  124.     if diff == 1:
  125.         for x in range(5):
  126.             answere = answerse[x].split(",")
  127.             print(x+1,".) ",questions[x])
  128.             print("A) ",answere[0])
  129.             print("B) ",answere[1])
  130.  
  131.             userAnswer = input("Answer: ")
  132.             if userAnswer.upper() == answere[2].replace("\n",""):
  133.                 score = score + 1
  134.  
  135.         print("You have answered ",((score*2)/10)*100,"% of questions correctly!")
  136.         print("You have earned ",score," points!")
  137.          
  138.     elif diff == 2:
  139.         for x in range(5):
  140.             answerm = answersm[x].split(",")
  141.             print(x+1,".) ",questions[x])
  142.             print("A) ",answerm[0])
  143.             print("B) ",answerm[1])
  144.             print("C) ",answerm[2])
  145.  
  146.             userAnswer = input("Answer: ")
  147.             if userAnswer.upper() == answerm[3].replace("\n",""):
  148.                 score = score + 1
  149.  
  150.         print("You have answered ",((score*2)/10)*100,"% of questions correctly!")
  151.         print("You have earned ",score," points!")
  152.  
  153.     elif diff == 3:
  154.         for x in range(5):
  155.             answerh = answersh[x].split(",")
  156.             print(x+1,".) ",questions[x])
  157.             print("A) ",answerh[0])
  158.             print("B) ",answerh[1])
  159.             print("C) ",answerh[2])
  160.             print("D) ",answerh[3])
  161.  
  162.             userAnswer = input("Answer: ")
  163.             if userAnswer.upper() == answerh[4].replace("\n",""):
  164.                 score = score + 1
  165.  
  166.         print("You have answered ",((score*2)/10)*100,"% of questions correctly!")
  167.         print("You have earned ",score," points!")
  168.  
  169.     else:
  170.         print("Wrong input.")
  171.         time.sleep(1)
  172.         history()
  173.  
  174.  
  175. # Program Init
  176. mainMenu()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement