Are there any ways I can clean this code up to make it more user-friendly?
import random #Random Function
import os
print("What is your name?") #Asking User Name
name = input().title() #.title() capitilises the first letter in each word, so when the name is printed in later code the first letter will always be capitilised.
class_name = input("Which class do you wish to input results for? ")
print (name, ", Welcome to the OCR Controlled Assessment Maths Test")
score = 0 #Set Score to 0
question = 0 #Set Question Number to 0
while question < 10: #After 10 Questions, it will stop asking the user questions.
operator = random.randint(1,3)#Addition,Multiplication,Subtraction
num1 = random.randint(1,25)#Random Number between 1 and 10 to be used in the questions
num2 = random.randint(1,25)#Random Number between 1 and 10 to be used in the questions
if operator == 1: #If Random Operator = 1, it will ask addition questions
print("What is", num1, "+", num2)
ans = num1 + num2
elif operator == 2:#If Random Operator = 2, it will ask multiplication questions
print("What is", num1, "*", num2)
ans = num1 * num2
else: #If Random Operator = 3/Else, it will ask subtraction questions.
print("What is", num1, "-", num2)
ans = num1 - num2
while True:
try:
user_ans = int(input()) #The Answer the user inputs to the question asked
except ValueError: #When anything but a interger is inputted.
print ("That is not a valid answer")
continue #Dosen't print Invalid Code Error
else:
break
if user_ans == ans: #If user answer matches that of the question
print("Correct")
score += 1
else: #If user answer dosen't match that of a question.
print("Incorrect")
question += 1 #When a question is answered, it will +1 until it equals 10 at which point it will stop.
print("Well done", name, "you scored", score, "/10") #Print Final Score out of 10
class_name = class_name + ".txt" #adds '.txt' to the end of the file so it can be used to create a file under the name a user specifies
file = open(class_name , 'a') #opens the file in 'append' mode so you don't delete all the information
name = (name)
file.write(str(name + " : " )) #writes the information to the file
file.write(str(score))
file.write('\n')
file.close() #safely closes the file to save the information
viewscore = input("Do you wish to view previous results for your class").lower()
if viewscore == "yes".lower():
f = open(class_name, 'r')
file_contents = f.read()
print (file_contents)
f.close()
elif viewscore != "yes".lower():
print ("Press any key to exit")
ex = input ("")
os._exit(0)
add additional functions
since we review existing code or implementing a new aspect in the code (the sort by alphanumeric). I would suggest you to clean your question of those elements. I would suggest you to visit the help center to better know how to ask a question here. – Marc-Andre Feb 2 at 14:48