Advertisement
Guest User

AlexGokhale Code

a guest
Feb 9th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.09 KB | None | 0 0
  1. import getpass
  2. import random
  3. lineUser = 0
  4. lineElec = 0
  5. lineNoUser = 0
  6. lineNoElec = 0
  7. elecFileContents = []
  8. compFileContents = []
  9. elecFileContentsHard= []
  10. compFileContentsHard = []
  11. pos = 0
  12. userScore = 0
  13. answerSelector1 = 0
  14. userFound = 0
  15. valid = 1
  16. line = "===================================================================================="
  17.  
  18. #Defines funtion display user's score, percentage and grade    
  19. def scoreAndGrade(userScore):
  20.     print("You got a score of: " + str(userScore))
  21.     percentage = (userScore / 5) * 100
  22.     print("Your percentage is: " + str(percentage) + "%")
  23.  
  24. print(line)
  25. print("Alex's QUIZ")
  26. print(line)
  27. print("Before you can play, you must register or login to your account.")
  28. #infinite loop
  29. while True:
  30.     print(line)
  31. #prompts user to login or register
  32.     registerOrLogin = int(input("Do you want to register (1) or login (2)? "))
  33. #if register is selected, collect user's details
  34.     if registerOrLogin == 1:
  35.         while True:
  36.             firstName = str(input("Please enter your forname: "))
  37.             if len(firstName) > 25:
  38.                 print("Error. Input too long.")
  39.             elif len(firstName) < 4:
  40.                 print("Error. Input too short.")
  41.             elif any(char.isdigit() for char in firstName):
  42.                 print("Error. Input has a number in.")
  43.             else:
  44.                 break
  45.         while True:
  46.             surname = str(input("Please enter your surname: "))
  47.             if len(surname) > 25:
  48.                 print("Error. Input too long.")
  49.             elif len(surname) < 4:
  50.                 print("Error. Input too short.")
  51.             elif any(char.isdigit() for char in surname):
  52.                 print("Error. Input has a number in.")
  53.             else:
  54.                 break
  55.         while True:
  56.             age = str(input("Please enter your age: "))
  57.             ageInt = int(age)
  58.             if ageInt > 120:
  59.                 print("Error. Input too high.")
  60.             elif ageInt < 6:
  61.                 print("Error. Input too low.")
  62.             else:
  63.                 break
  64.         while True:
  65.             yearGroup = int(input("Enter your year group (just two numbers e.g. Yr7 is 07): "))
  66.             if yearGroup > 13:
  67.                 print("Error. Year group too high.")
  68.             elif yearGroup < 1:
  69.                 print("Error. Year group too low.")
  70.             else:
  71.                 break
  72. #truncates first name to first 3 letters
  73.         letters = (firstName[0:3])
  74. #creates username by adding age to the 3 letters
  75.         username = letters + str(age)
  76.         print("Your username is: "  + username)
  77.         password = str(input("Enter Password: "))
  78. #opens file which stores user details in append mode
  79.         file = open("userDetails.txt", "a")
  80. #writes the new user's data to file
  81.         file.write(firstName + "\n" + surname + "\n" + str(age) + "\n" + str(yearGroup) + "\n" + username + "\n" + password + "\n\n")
  82. #closes the file
  83.         file.close()
  84.         print("Your details have been saved.")
  85. #if login is selected prompt user to enter username and password
  86.     elif registerOrLogin == 2:
  87.         userCheck = input("Enter your username: ")
  88. #opens user details file in read only mode
  89.         file = open("userDetails.txt", "r")
  90. #for every line in the file
  91.         for lineUser in file:
  92.             lineNoUser +=1
  93. #if selected line is the same as the user's input then
  94.             if lineUser == (userCheck + '\n'):
  95. #prompt input of password
  96.                 passCheck = input("Enter your password: ")
  97. #adds a '\n' to the user's input because each line in the file is on a new line
  98.                 passCheck = passCheck + '\n'
  99. #reads the line in the file below the username (which contains the password)
  100.                 password = file.readline(lineNoUser + 1)
  101. #if the user's input is the same as the password stored on file then...
  102.                 if passCheck == password:
  103.                     print("Login Successful.")
  104. #offer the user a choice of subject and difficulty
  105.                     topicChoice = int(input("Do you want to take a quiz on electronics(1) or computer science(2)? "))
  106.                     difficulty = str(input("Do you want to play in easy(e), medium(m) or hard(h) mode? "))
  107.                     print(line)
  108.                     #closes the file
  109.                     file.close()
  110. #exits the loop which checks each line
  111.                     break
  112. #exits the infinite loop
  113.         break
  114.  
  115.  
  116.                    
  117.            
  118.                
  119. if topicChoice == 1:
  120.     electronics = open("electronicsQuestions.txt", 'r')
  121.     for x in range(0,5):
  122.         randomQuestion = 'Q' + str(random.randint(1,10))
  123.         for lineElec in electronics:
  124.             elecFileContents.append(lineElec[:len(lineElec)-1])
  125.         for entryElec in elecFileContents:
  126.             pos +=1
  127.             if entryElec == randomQuestion:
  128.                 print(elecFileContents[pos])
  129.                 print(line)
  130.                 break
  131.         if difficulty == 'e':
  132.             correctAnswer = elecFileContents[pos + 1]
  133.             correctAnswerLetter = (correctAnswer[:1])
  134.             answerSelector = random.randint(1, 3)
  135.             if answerSelector == 1:
  136.                 otherAnswer = elecFileContents[pos + 2]
  137.             elif answerSelector == 2:
  138.                 otherAnswer = elecFileContents[pos + 3]
  139.             elif answerSelector == 3:
  140.                 otherAnswer = elecFileContents[pos + 4]
  141.             questionOrder = random.randint(1,2)
  142.             if questionOrder == 1:
  143.                 print(correctAnswer)
  144.                 print(otherAnswer)
  145.                 print(line)
  146.             else:
  147.                 print(otherAnswer)
  148.                 print(correctAnswer)
  149.                 print(line)
  150.             userAnswer = str(input("Which answer do you think is correct? Type in the letter (A, B, C or D): "))
  151.             print(line)
  152.             if userAnswer == correctAnswerLetter:
  153.                 userScore +=1
  154.                 print("Well done! That was the right answer.")
  155.                 print(line)
  156.                 pos = 0
  157.             else:
  158.                 print("Sorry, that answer was incorrect.")
  159.                 print(line)
  160.                 pos = 0
  161.         if difficulty == 'h':
  162.             correctAnswer = elecFileContents[pos+1]
  163.             correctAnswerLetter = (correctAnswer[:1])
  164.             electronics.close()
  165.             electronics = open("electronicsQuestionsHard.txt", 'r')
  166.             for lineElec in electronics:
  167.                 elecFileContentsHard.append(lineElec[:len(lineElec)-1])
  168.             print(elecFileContentsHard[pos+1])
  169.             print(elecFileContentsHard[pos+2])
  170.             print(elecFileContentsHard[pos+3])
  171.             print(elecFileContentsHard[pos+4])
  172.             print(line)
  173.             userAnswer = str(input("Which answer do you think is correct? Type in the letter (A, B, C or D): "))
  174.             print(line)
  175.             if userAnswer == correctAnswerLetter:
  176.                 userScore +=1
  177.                 print("Well done! That was the right answer.")
  178.                 print(line)
  179.                 pos = 0
  180.             else:
  181.                 print("Sorry, that answer was incorrect.")
  182.                 print(line)
  183.                 pos = 0
  184.  
  185.     scoreAndGrade(userScore)
  186.     electronics.close()
  187.            
  188. if topicChoice == 2:
  189.     computing = open("computingQuestions.txt", 'r')
  190.     for x in range(0,5):
  191.         randomQuestion = 'Q' + str(random.randint(1,10))
  192.         for lineComp in computing:
  193.             compFileContents.append(lineComp[:len(lineComp)-1])
  194.         for entryComp in compFileContents:
  195.             pos +=1
  196.             if entryComp == randomQuestion:
  197.                 print(compFileContents[pos])
  198.                 print(line)
  199.                 break
  200.         if difficulty == 'e':
  201.             correctAnswer = compFileContents[pos + 1]
  202.             correctAnswerLetter = (correctAnswer[:1])
  203.             answerSelector = random.randint(1, 3)
  204.             if answerSelector == 1:
  205.                 otherAnswer = compFileContents[pos + 2]
  206.             elif answerSelector == 2:
  207.                 otherAnswer = compFileContents[pos + 3]
  208.             elif answerSelector == 3:
  209.                 otherAnswer = compFileContents[pos + 4]
  210.             questionOrder = random.randint(1,2)
  211.             if questionOrder == 1:
  212.                 print(correctAnswer)
  213.                 print(otherAnswer)
  214.                 print(line)
  215.             else:
  216.                 print(otherAnswer)
  217.                 print(correctAnswer)
  218.                 print(line)
  219.             userAnswer = str(input("Which answer do you think is correct? Type in the letter (A, B, C or D): "))
  220.             print(line)
  221.             if userAnswer == correctAnswerLetter:
  222.                 userScore +=1
  223.                 print("Well done! That was the right answer.")
  224.                 print(line)
  225.                 pos = 0
  226.             else:
  227.                 print("Sorry, that answer was incorrect.")
  228.                 print(line)
  229.                 pos = 0
  230.         if difficulty == 'h':
  231.             correctAnswer = compFileContents[pos+1]
  232.             correctAnswerLetter = (correctAnswer[:1])
  233.             computing.close()
  234.             computing = open("computingQuestionsHard.txt", 'r')
  235.             for lineComp in computing:
  236.                 compFileContentsHard.append(lineComp[:len(lineComp)-1])
  237.             print(compFileContentsHard[pos+1])
  238.             print(compFileContentsHard[pos+2])
  239.             print(compFileContentsHard[pos+3])
  240.             print(compFileContentsHard[pos+4])
  241.             print(line)
  242.             userAnswer = str(input("Which answer do you think is correct? Type in the letter (A, B, C or D): "))
  243.             print(line)
  244.             if userAnswer == correctAnswerLetter:
  245.                 userScore +=1
  246.                 print("Well done! That was the right answer.")
  247.                 print(line)
  248.                 pos = 0
  249.             else:
  250.                 print("Sorry, that answer was incorrect.")
  251.                 print(line)
  252.                 pos = 0
  253.  
  254.     scoreAndGrade(userScore)
  255.     computing.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement