Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import getpass
- import random
- lineUser = 0
- lineElec = 0
- lineNoUser = 0
- lineNoElec = 0
- elecFileContents = []
- compFileContents = []
- elecFileContentsHard= []
- compFileContentsHard = []
- pos = 0
- userScore = 0
- answerSelector1 = 0
- userFound = 0
- valid = 1
- line = "===================================================================================="
- #Defines funtion display user's score, percentage and grade
- def scoreAndGrade(userScore):
- print("You got a score of: " + str(userScore))
- percentage = (userScore / 5) * 100
- print("Your percentage is: " + str(percentage) + "%")
- print(line)
- print("Alex's QUIZ")
- print(line)
- print("Before you can play, you must register or login to your account.")
- #infinite loop
- while True:
- print(line)
- #prompts user to login or register
- registerOrLogin = int(input("Do you want to register (1) or login (2)? "))
- #if register is selected, collect user's details
- if registerOrLogin == 1:
- while True:
- firstName = str(input("Please enter your forname: "))
- if len(firstName) > 25:
- print("Error. Input too long.")
- elif len(firstName) < 4:
- print("Error. Input too short.")
- elif any(char.isdigit() for char in firstName):
- print("Error. Input has a number in.")
- else:
- break
- while True:
- surname = str(input("Please enter your surname: "))
- if len(surname) > 25:
- print("Error. Input too long.")
- elif len(surname) < 4:
- print("Error. Input too short.")
- elif any(char.isdigit() for char in surname):
- print("Error. Input has a number in.")
- else:
- break
- while True:
- age = str(input("Please enter your age: "))
- ageInt = int(age)
- if ageInt > 120:
- print("Error. Input too high.")
- elif ageInt < 6:
- print("Error. Input too low.")
- else:
- break
- while True:
- yearGroup = int(input("Enter your year group (just two numbers e.g. Yr7 is 07): "))
- if yearGroup > 13:
- print("Error. Year group too high.")
- elif yearGroup < 1:
- print("Error. Year group too low.")
- else:
- break
- #truncates first name to first 3 letters
- letters = (firstName[0:3])
- #creates username by adding age to the 3 letters
- username = letters + str(age)
- print("Your username is: " + username)
- password = str(input("Enter Password: "))
- #opens file which stores user details in append mode
- file = open("userDetails.txt", "a")
- #writes the new user's data to file
- file.write(firstName + "\n" + surname + "\n" + str(age) + "\n" + str(yearGroup) + "\n" + username + "\n" + password + "\n\n")
- #closes the file
- file.close()
- print("Your details have been saved.")
- #if login is selected prompt user to enter username and password
- elif registerOrLogin == 2:
- userCheck = input("Enter your username: ")
- #opens user details file in read only mode
- file = open("userDetails.txt", "r")
- #for every line in the file
- for lineUser in file:
- lineNoUser +=1
- #if selected line is the same as the user's input then
- if lineUser == (userCheck + '\n'):
- #prompt input of password
- passCheck = input("Enter your password: ")
- #adds a '\n' to the user's input because each line in the file is on a new line
- passCheck = passCheck + '\n'
- #reads the line in the file below the username (which contains the password)
- password = file.readline(lineNoUser + 1)
- #if the user's input is the same as the password stored on file then...
- if passCheck == password:
- print("Login Successful.")
- #offer the user a choice of subject and difficulty
- topicChoice = int(input("Do you want to take a quiz on electronics(1) or computer science(2)? "))
- difficulty = str(input("Do you want to play in easy(e), medium(m) or hard(h) mode? "))
- print(line)
- #closes the file
- file.close()
- #exits the loop which checks each line
- break
- #exits the infinite loop
- break
- if topicChoice == 1:
- electronics = open("electronicsQuestions.txt", 'r')
- for x in range(0,5):
- randomQuestion = 'Q' + str(random.randint(1,10))
- for lineElec in electronics:
- elecFileContents.append(lineElec[:len(lineElec)-1])
- for entryElec in elecFileContents:
- pos +=1
- if entryElec == randomQuestion:
- print(elecFileContents[pos])
- print(line)
- break
- if difficulty == 'e':
- correctAnswer = elecFileContents[pos + 1]
- correctAnswerLetter = (correctAnswer[:1])
- answerSelector = random.randint(1, 3)
- if answerSelector == 1:
- otherAnswer = elecFileContents[pos + 2]
- elif answerSelector == 2:
- otherAnswer = elecFileContents[pos + 3]
- elif answerSelector == 3:
- otherAnswer = elecFileContents[pos + 4]
- questionOrder = random.randint(1,2)
- if questionOrder == 1:
- print(correctAnswer)
- print(otherAnswer)
- print(line)
- else:
- print(otherAnswer)
- print(correctAnswer)
- print(line)
- userAnswer = str(input("Which answer do you think is correct? Type in the letter (A, B, C or D): "))
- print(line)
- if userAnswer == correctAnswerLetter:
- userScore +=1
- print("Well done! That was the right answer.")
- print(line)
- pos = 0
- else:
- print("Sorry, that answer was incorrect.")
- print(line)
- pos = 0
- if difficulty == 'h':
- correctAnswer = elecFileContents[pos+1]
- correctAnswerLetter = (correctAnswer[:1])
- electronics.close()
- electronics = open("electronicsQuestionsHard.txt", 'r')
- for lineElec in electronics:
- elecFileContentsHard.append(lineElec[:len(lineElec)-1])
- print(elecFileContentsHard[pos+1])
- print(elecFileContentsHard[pos+2])
- print(elecFileContentsHard[pos+3])
- print(elecFileContentsHard[pos+4])
- print(line)
- userAnswer = str(input("Which answer do you think is correct? Type in the letter (A, B, C or D): "))
- print(line)
- if userAnswer == correctAnswerLetter:
- userScore +=1
- print("Well done! That was the right answer.")
- print(line)
- pos = 0
- else:
- print("Sorry, that answer was incorrect.")
- print(line)
- pos = 0
- scoreAndGrade(userScore)
- electronics.close()
- if topicChoice == 2:
- computing = open("computingQuestions.txt", 'r')
- for x in range(0,5):
- randomQuestion = 'Q' + str(random.randint(1,10))
- for lineComp in computing:
- compFileContents.append(lineComp[:len(lineComp)-1])
- for entryComp in compFileContents:
- pos +=1
- if entryComp == randomQuestion:
- print(compFileContents[pos])
- print(line)
- break
- if difficulty == 'e':
- correctAnswer = compFileContents[pos + 1]
- correctAnswerLetter = (correctAnswer[:1])
- answerSelector = random.randint(1, 3)
- if answerSelector == 1:
- otherAnswer = compFileContents[pos + 2]
- elif answerSelector == 2:
- otherAnswer = compFileContents[pos + 3]
- elif answerSelector == 3:
- otherAnswer = compFileContents[pos + 4]
- questionOrder = random.randint(1,2)
- if questionOrder == 1:
- print(correctAnswer)
- print(otherAnswer)
- print(line)
- else:
- print(otherAnswer)
- print(correctAnswer)
- print(line)
- userAnswer = str(input("Which answer do you think is correct? Type in the letter (A, B, C or D): "))
- print(line)
- if userAnswer == correctAnswerLetter:
- userScore +=1
- print("Well done! That was the right answer.")
- print(line)
- pos = 0
- else:
- print("Sorry, that answer was incorrect.")
- print(line)
- pos = 0
- if difficulty == 'h':
- correctAnswer = compFileContents[pos+1]
- correctAnswerLetter = (correctAnswer[:1])
- computing.close()
- computing = open("computingQuestionsHard.txt", 'r')
- for lineComp in computing:
- compFileContentsHard.append(lineComp[:len(lineComp)-1])
- print(compFileContentsHard[pos+1])
- print(compFileContentsHard[pos+2])
- print(compFileContentsHard[pos+3])
- print(compFileContentsHard[pos+4])
- print(line)
- userAnswer = str(input("Which answer do you think is correct? Type in the letter (A, B, C or D): "))
- print(line)
- if userAnswer == correctAnswerLetter:
- userScore +=1
- print("Well done! That was the right answer.")
- print(line)
- pos = 0
- else:
- print("Sorry, that answer was incorrect.")
- print(line)
- pos = 0
- scoreAndGrade(userScore)
- computing.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement