This is a program I have been working on in python. It is meant to be a quiz to some secondary school pupils on two subjects/topics which are maths and history.
So far, it allows a user to try the quiz on one of the subjects and gives them a grade, percentage and score according to what they have answered correctly in the quiz. However, the program seems long-winded with two very big functions as well:
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()
How can I make the program more efficient (i.e. fewer lines of code) and possibly not as repetitive?
maths.txt
look like? \$\endgroup\$