Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- print("Welcome to my quiz!")
- def maths():
- with open("maths.txt","r") as topic1:
- score = 0
- difficultyLevel = input("Please select a difficulty level for the maths quiz:easy, medium or hard:")
- questionsForMaths = topic1.readlines()
- print("The maths questions:")
- if difficultyLevel == "Easy" or difficultyLevel == "easy":
- for x in range(0,3):
- print(questionsForMaths[x].rstrip())
- userAnswer = input("Choose from the following:").lower()
- if userAnswer == questionsForMaths[1].rstrip():
- print ("correct")
- score = score + 1
- else:
- print ("incorrect")
- #2nd question
- for x in range(5,8):
- print(questionsForMaths[x].rstrip())
- userAnswer = input("Choose from the following:").lower()
- if userAnswer == questionsForMaths[7].rstrip():
- print ("correct")
- score = score + 1
- else:
- print ("incorrect")
- #3rd question
- for x in range(10,13):
- print(questionsForMaths[x].rstrip())
- userAnswer = input("Choose from the following:").lower()
- if userAnswer == questionsForMaths[12].rstrip():
- print ("correct")
- score = score + 1
- else:
- print ("incorrect")
- #4th question
- for x in range(15,18):
- print(questionsForMaths[x].rstrip())
- userAnswer = input("Choose from the following:").lower()
- if userAnswer == questionsForMaths[16].rstrip():
- print("correct")
- score = score + 1
- else:
- print("incorrect")
- #5th question
- for x in range(20,23):
- print(questionsForMaths[x].rstrip())
- userAnswer = input("Choose from the following:")
- if userAnswer == questionsForMaths[22].rstrip():
- print("correct")
- score = score + 1
- else:
- print("incorrect")
- elif difficultyLevel == "Medium" or difficultyLevel == "medium":
- for x in range(0,4):
- print(questionsForMaths[x].rstrip())
- userAnswer = input("Choose from the following:").lower()
- if userAnswer == questionsForMaths[1].rstrip():
- print ("correct")
- score = score + 1
- else:
- print ("incorrect")
- #2nd question
- for x in range(5,9):
- print(questionsForMaths[x].rstrip())
- userAnswer = input("Choose from the following:").lower()
- if userAnswer == questionsForMaths[7].rstrip():
- print ("correct")
- score = score + 1
- else:
- print ("incorrect")
- #3rd question
- for x in range(10,14):
- print(questionsForMaths[x].rstrip())
- userAnswer = input("Choose from the following:").lower()
- if userAnswer == questionsForMaths[12].rstrip():
- print ("correct")
- score = score + 1
- else:
- print ("incorrect")
- #4th question
- for x in range(15,19):
- print(questionsForMaths[x].rstrip())
- userAnswer = input("Choose from the following:").lower()
- if userAnswer == questionsForMaths[16].rstrip():
- print("correct")
- score = score + 1
- else:
- print("incorrect")
- #5th question
- for x in range(20,24):
- print(questionsForMaths[x].rstrip())
- userAnswer = input("Choose from the following:")
- if userAnswer == questionsForMaths[22].rstrip():
- print("correct")
- score = score + 1
- else:
- print("incorrect")
- elif difficultyLevel == "Hard" or difficultyLevel == "hard":
- for x in range(0,5):
- print(questionsForMaths[x].rstrip())
- userAnswer = input("Choose from the following:").lower()
- if userAnswer == questionsForMaths[1].rstrip():
- print ("correct")
- score = score + 1
- else:
- print ("incorrect")
- #2nd question
- for x in range(5,10):
- print(questionsForMaths[x].rstrip())
- userAnswer = input("Choose from the following:").lower()
- if userAnswer == questionsForMaths[7].rstrip():
- print ("correct")
- score = score + 1
- else:
- print ("incorrect")
- #3rd question
- for x in range(10,15):
- print(questionsForMaths[x].rstrip())
- userAnswer = input("Choose from the following:").lower()
- if userAnswer == questionsForMaths[12].rstrip():
- print ("correct")
- score = score + 1
- else:
- print ("incorrect")
- #4th question
- for x in range(15,20):
- print(questionsForMaths[x].rstrip())
- userAnswer = input("Choose from the following:").lower()
- if userAnswer == questionsForMaths[16].rstrip():
- print("correct")
- score = score + 1
- else:
- print("incorrect")
- #5th question
- for x in range(20,25):
- print(questionsForMaths[x].rstrip())
- userAnswer = input("Choose from the following:")
- if userAnswer == questionsForMaths[22].rstrip():
- print("correct")
- score = score + 1
- else:
- print("incorrect")
- print("Your total score is", score)
- percentage = (score/5)*100
- print("The percentage of questions correct is", percentage)
- if percentage < 40.0:
- print("You have failed the maths quiz")
- with open("reports.txt","a") as reports:
- reports.write("n" + fullUsername + " " + "achieved a grade of fail" + " " + "in the maths quiz")
- elif percentage >= 40.0 and percentage <= 60.0:
- print("You have passed the maths quiz")
- with open("reports.txt","a") as reports:
- reports.write("n" + fullUsername + " " + "achieved a grade of pass" + " " + "in the maths quiz")
- elif percentage >= 60.0 and percentage <= 80.0:
- print("You have achieved merit for the maths quiz")
- with open("reports.txt","a") as reports:
- reports.write("n" + fullUsername + " " + "achieved a grade of merit" + " " + "in the maths quiz")
- elif percentage > 80.0:
- print("You have achieved distinction for maths quiz")
- with open("reports.txt","a") as reports:
- reports.write("n" + fullUsername + " " + "achieved a grade of distinction" + " " + "in the maths quiz")
- with open("reports.txt","a") as reports:
- reports.write("n" + fullUsername + " " + "did the maths quiz and" + " " + "achieved a score of" + " " + str(score))
- reports.write("n" + fullUsername + " " + "did the maths quiz and" + " " + "achieved a percentage of" + " " + str(percentage))
- def history():
- with open("history.txt","r") as topic2:
- score = 0
- print("The history questions:")
- difficultyLevel = input("Please select a difficulty level for the history quiz:easy, medium or hard:")
- questionsForHistory = topic2.readlines()
- if difficultyLevel == "Easy" or difficultyLevel == "easy":
- for y in range(0,3):
- print(questionsForHistory[y].rstrip())
- userAnswer2 = input("Choose from the following:")
- if userAnswer2 == questionsForHistory[2].rstrip():
- print("correct")
- score = score + 1
- else:
- print("incorrect")
- #2nd question
- for y in range(5,8):
- print(questionsForHistory[y].rstrip())
- userAnswer2 = input("Choose from the following:")
- if userAnswer2 == questionsForHistory[6].rstrip():
- print("correct")
- score = score + 1
- else:
- print("incorrect")
- #3rd question
- for y in range(10,13):
- print(questionsForHistory[y].rstrip())
- userAnswer2 = input("Choose from the following:")
- if userAnswer2 == questionsForHistory[12].rstrip() or userAnswer2 == questionsForHistory[12].rstrip().lower():
- print("correct")
- score = score + 1
- else:
- print("incorrect")
- #4th question
- for y in range(15,18):
- print(questionsForHistory[y].rstrip())
- userAnswer2 = input("Choose from the following:")
- if userAnswer2 == questionsForHistory[17].rstrip():
- print("correct")
- score = score + 1
- else:
- print("incorrect")
- #5th question
- for y in range(20,23):
- print(questionsForHistory[y].rstrip())
- userAnswer2 = input("Choose from the following:")
- if userAnswer2 == questionsForHistory[21].rstrip():
- print("correct")
- score = score + 1
- else:
- print("incorrect")
- elif difficultyLevel == "Medium" or difficultyLevel == "medium":
- for y in range(0,4):
- print(questionsForHistory[y].rstrip())
- userAnswer2 = input("Choose from the following:")
- if userAnswer2 == questionsForHistory[2].rstrip():
- print("correct")
- score = score + 1
- else:
- print("incorrect")
- #2nd question
- for y in range(5,9):
- print(questionsForHistory[y].rstrip())
- userAnswer2 = input("Choose from the following:")
- if userAnswer2 == questionsForHistory[6].rstrip():
- print("correct")
- score = score + 1
- else:
- print("incorrect")
- #3rd question
- for y in range(10,14):
- print(questionsForHistory[y].rstrip())
- userAnswer2 = input("Choose from the following:")
- if userAnswer2 == questionsForHistory[12].rstrip() or userAnswer2 == questionsForHistory[12].rstrip().lower():
- print("correct")
- score = score + 1
- else:
- print("incorrect")
- #4th question
- for y in range(15,19):
- print(questionsForHistory[y].rstrip())
- userAnswer2 = input("Choose from the following:")
- if userAnswer2 == questionsForHistory[17].rstrip():
- print("correct")
- score = score + 1
- else:
- print("incorrect")
- #5th question
- for y in range(20,24):
- print(questionsForHistory[y].rstrip())
- userAnswer2 = input("Choose from the following:")
- if userAnswer2 == questionsForHistory[21].rstrip():
- print("correct")
- score = score + 1
- else:
- print("incorrect")
- elif difficultyLevel == "Hard" or difficultyLevel == "hard":
- for y in range(0,5):
- print(questionsForHistory[y].rstrip())
- userAnswer2 = input("Choose from the following:")
- if userAnswer2 == questionsForHistory[2].rstrip():
- print("correct")
- score = score + 1
- else:
- print("incorrect")
- #2th question
- for y in range(5,10):
- print(questionsForHistory[y].rstrip())
- userAnswer2 = input("Choose from the following:")
- if userAnswer2 == questionsForHistory[6].rstrip():
- print("correct")
- score = score + 1
- else:
- print("incorrect")
- #3rd question
- for y in range(10,15):
- print(questionsForHistory[y].rstrip())
- userAnswer2 = input("Choose from the following:")
- if userAnswer2 == questionsForHistory[12].rstrip() or userAnswer2 == questionsForHistory[12].rstrip().lower():
- print("correct")
- score = score + 1
- else:
- print("incorrect")
- #4th question
- for y in range(15,20):
- print(questionsForHistory[y].rstrip())
- userAnswer2 = input("Choose from the following:")
- if userAnswer2 == questionsForHistory[17].rstrip():
- print("correct")
- score = score + 1
- else:
- print("incorrect")
- #5th question
- for y in range(20,25):
- print(questionsForHistory[y].rstrip())
- userAnswer2 = input("Choose from the following:")
- if userAnswer2 == questionsForHistory[21].rstrip():
- print("correct")
- score = score + 1
- else:
- print("incorrect")
- print("Your total score is", score)
- percentage = (score/5)*100
- print("The percentage of questions correct is", percentage)
- if percentage < 40.0:
- print("You have failed the history quiz")
- with open("reports.txt","a") as reports:
- reports.write("n" + fullUsername + " " + "achieved a grade of fail" + " " + "in the history quiz")
- elif percentage >= 40.0 and percentage <= 60.0:
- print("You have passed the history quiz")
- with open("reports.txt","a") as reports:
- reports.write("n" + fullUsername + " " + "achieved a grade of pass" + " " + "in the history quiz")
- elif percentage >= 60.0 and percentage <= 80.0:
- print("You have achieved merit for the history quiz")
- with open("reports.txt","a") as reports:
- reports.write("n" + fullUsername + " " + "achieved a grade of merit" + " " + "in the history quiz")
- elif percentage > 80.0:
- print("You have achieved distinction for history quiz")
- with open("reports.txt","a") as reports:
- reports.write("n" + fullUsername + " " + "achieved a grade of distinction" + " " + "in the history quiz")
- with open("reports.txt","a") as reports:
- reports.write("n" + fullUsername + " " + "did the history quiz and" + " " + "achieved a score of" + " " + str(score))
- reports.write("n" + fullUsername + " " + "did the history quiz and" + " " + "achieved a percentage of" + " " + str(percentage))
- with open("user account .txt","w") as userFile:
- usernamePart1 = input("Enter your name:")
- while not usernamePart1.isalpha():
- print("Invalid name, try again")
- usernamePart1 = input("Enter your name:")
- usernamePart2 = input("Enter your age:")
- while not usernamePart2.isdigit():
- print("try again")
- usernamePart2 = input("Enter your age:")
- fullUsername = usernamePart1[:3] + usernamePart2
- userFile.write("Username:" + fullUsername)
- with open("reports.txt","a") as reports:
- reports.write("n" + "Username:" + fullUsername)
- print(fullUsername)
- UserYearGroup = int(input("Enter your year group:"))
- while UserYearGroup < 7 or UserYearGroup > 11:
- print("Invalid year group, enter again")
- UserYearGroup = int(input("Enter your year group:"))
- if UserYearGroup >= 7 and UserYearGroup <= 11:
- userFile.write("nYear Group:" + str(UserYearGroup))
- print(UserYearGroup)
- password = input("Enter your password, it must be at least 6 characters long and no more than 11 characters:")
- while len(password) < 6 or len(password) > 11:
- print("Invalid password, try again:")
- password = input("Enter your password, it must be at least 6 characters long and no more than 11 characters:")
- if len(password) >= 6 and len(password) <= 11:
- userFile.write("nPassword:" + password)
- topicChoice = input("Which topic do you want to test yourself on : its either maths or history?")
- his = "history".lower()
- ma = "maths".lower()
- if topicChoice == his:
- history()
- elif topicChoice == ma:
- maths()
- user_account_filename = "user account .txt"
- report_filename = "reports.txt"
- expectedAnswers = {
- 'maths':[1,7,12,16,22],
- 'history':[2,6,12,17,21]
- }
- level2questions = {
- 'easy':3,
- 'medium':4,
- 'hard':5
- }
- question_num = 5
- def start_test(topic, fullUsername):
- score = 0
- questions_filename = topic + '.txt'
- questions = open(questions_filename).readlines()
- difficultyLevel = input("Please select a difficulty level for the " + topic + " quiz:easy, medium or hard:")
- if difficultyLevel.lower in ["easy","medium","hard"]:
- difficultyLevel = difficultyLevel.lower()
- print("The " + topic + " questions:")
- for i in range(question_num):
- for x in range(i * 5, i * 5 + level2questions[difficultyLevel]):
- print(questions[x].rstrip())
- userAnswer = input("Choose from the following:").lower()
- if i == 2 and topic == "history":
- userAnswer2 = userAnswer2.lower()
- if userAnswer == questions[expectedAnswers[topic][i]].rstrip():
- print ("correct")
- score = score + 1
- else:
- print ("incorrect")
- #TODO:else
- print("Your total score is", score)
- test_result(score, topic, fullUsername)
- def test_result(score, topic, fullUsername):
- def test_result(result):
- report = "n" + fullUsername + " achieved a grade of " + result + " in the " + topic + " quiz"
- if result in ["merit", "distinction"]:
- result = "achieved " + result + " for"
- else:
- result += "ed"
- printout = "You have " + result + " the " + topic + " quiz"
- return printout, report
- percentage = (score/5)*100
- print("The percentage of questions correct is", percentage)
- if percentage < 40.0:
- result = "fail"
- elif percentage >= 40.0 and percentage <= 60.0:
- result = "pass"
- elif percentage >= 60.0 and percentage <= 80.0:
- result = "merit"
- elif percentage > 80.0:
- result = "distinction"
- printout, report = test_result(result)
- print(printout)
- record_prefix = "n" + fullUsername + " did the " + topic + " quiz and achieved a "
- write_file(report_filename, report + record_prefix + "score of " + str(score) + record_prefix + "percentage of " + str(percentage))
- def write_file(path,data):
- with open(path,'a+') as f:
- f.write(data)
- def main():
- print("Welcome to my quiz!")
- #fullUsername
- usernamePart1, usernamePart2 = "",""
- while not usernamePart1.isalpha():
- print("Invalid name, try again")
- usernamePart1 = input("Enter your name:")
- while not usernamePart2.isdigit():
- print("try again")
- usernamePart2 = input("Enter your age:")
- fullUsername = usernamePart1[:3] + usernamePart2
- write_file(user_account_filename, "Username:" + fullUsername)
- write_file(report_filename, "nUsername:" + fullUsername)
- print(fullUsername)
- #UserYearGroup
- #I have modified some code here, add `UserYearGroup.isdigit()` and delete the `if UserYearGroup >= 7 and UserYearGroup <= 11`
- UserYearGroup = 0
- while UserYearGroup.isdigit() and not (7 <= int(UserYearGroup) <= 11):
- print("Invalid year group, enter again")
- UserYearGroup = input("Enter your year group:")
- write_file(user_account_filename, "nYear Group:" + UserYearGroup)
- print(UserYearGroup)
- #Password
- password = ""
- while not 6 <= len(password) <= 11:
- print("Invalid password, try again:")
- password = input("Enter your password, it must be at least 6 characters long and no more than 11 characters:")
- write_file(user_account_filename, "nPassword:" + password)
- #TopicChoice
- topicChoice = input("Which topic do you want to test yourself on : its either maths or history?")
- #deleted `his` and `ma`
- if topicChoice.lower() in ["history","maths"]:
- start_test(topicChoice, fullUsername)
- #TODO:else
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment