# Arithmetic quiz with class database and score analysis

I have done this quiz project and I have tried as hard as I could when I was writing to make it user friendly to read. Could you help me on improving and making it more user friendly please.

import random
import operator
import csv

OPERATIONS = [
(operator.mul, "*"),
(operator.sub, "-")
]

NB_QUESTIONS = 10

def get_int_input(prompt=''):
while True:
try:
return int(input(prompt))
except ValueError:
print("Not a valid input (integer is expected)")

def get_bool_input(prompt=''):
while True:
val = input(prompt).lower()
if val == 'yes':
return True
elif val == 'no':
return False
else:
print("Not a valid input (yes/no is expected)")

if __name__ == '__main__':
name = input("What is your name?").title()
print(name, ", Welcome to the OCR Controlled Assessment Maths Test")

score = 0
for _ in range(NB_QUESTIONS):
num1 = random.randint(1,11)
num2 = random.randint(1,11)
op, symbol = random.choice(OPERATIONS)
print("What is", num1, symbol, num2)
if get_int_input() == op(num1, num2):
print("Correct")
score += 1
else:
print("Incorrect")

print("Well done", name, "you scored", score, "/", NB_QUESTIONS)

print("Are you in class 7, 8 or 9")
student_class = int(input())

if student_class>9:
print("This is not a class")
student_class = int(input())
if student_class<7:
print("This is not a class")
student_class = int(input())

if student_class == 7:
print('Enter your score for test 2')
test2 = int(input())
print('Enter your score for test 3')
test3 = int(input())
with open('class7.csv', 'a', newline='') as fp:
a = csv.writer(fp,delimiter=',')
data=[[name,score,test2,test3]]
a.writerows(data)
f = open('class7.csv')
newlist = []
for row in csv_f:
row = int(row)
row = int(row)
row = int(row)
minimum = min(row[1:4])
row.append(minimum)
maximum = max(row[1:4])
row.append(maximum)
average = round(sum(row[1:4])/3)
row.append(average)
newlist.append(row[0:7])
alphabetical = [[x, x] for x in newlist]
for entry in sorted(alphabetical):
print(entry)

Highscore = [[x, x]for x in newlist]
print('Highest scores')
for entry in sorted(Highscore, reverse = True) :
print(entry)

averagescore = [[x, x] for x in newlist]
print('Average scores')
for entry in sorted(averagescore, reverse = True) :
print(entry)

if student_class == 8:
print('Enter your score for test 2')
test2 = int(input())
print('Enter your score for test 3')
test3 = int(input())
with open('class8.csv','a',newline='') as fp:
a = csv.writer(fp,delimiter=',')
data=[[name,score,test2,test3]]
a.writerows(data)
f = open('class8.csv')
newlist = []
for row in csv_f:
row = int(row)
row = int(row)
row = int(row)
minimum = min(row[1:4])
row.append(minimum)
maximum = max(row[1:4])
row.append(maximum)
average = round(sum(row[1:4])/3)
row.append(average)
newlist.append(row[0:7])
alphabetical = [[x, x] for x in newlist]
for entry in sorted(alphabetical):
print(entry)

Highscore = [[x, x]for x in newlist]
print('Highest scores')
for entry in sorted(Highscore, reverse = True) :
print(entry)

averagescore = [[x, x] for x in newlist]
print('Average scores')
for entry in sorted(averagescore, reverse = True) :
print(entry)

if student_class == 9:
print('Enter your score for test 2')
test2 = int(input())
print('Enter your score for test 3')
test3 = int(input())
with open('class9.csv','a',newline='') as fp:
a = csv.writer(fp, delimiter=',')
data=[[name,score,test2,test3]]
a.writerows(data)
f = open('class9.csv')
newlist = []
for row in csv_f:
row = int(row)
row = int(row)
row = int(row)
minimum = min(row[1:4])
row.append(minimum)
maximum = max(row[1:4])
row.append(maximum)
average = round(sum(row[1:4])/3)
row.append(average)
newlist.append(row[0:7])
alphabetical = [[x, x] for x in newlist]
for entry in sorted(alphabetical):
print(entry)

Highscore = [[x, x]for x in newlist]
print('Highest scores')
for entry in sorted(Highscore, reverse = True) :
print(entry)

averagescore = [[x, x] for x in newlist]
print('Average scores')
for entry in sorted(averagescore, reverse = True) :
print(entry)


A major step towards better readability of your code would be to introduce docstrings (for an "official" guideline see PEP257) or at least some comments where you explain your intension.

Your check where the user inputs his class will fail if the second input of the user is still invalid.

print("Are you in class 7, 8 or 9")
student_class = int(input())

if student_class>9:
print("This is not a class")
student_class = int(input())
if student_class<7:
print("This is not a class")
student_class = int(input())


As none of the three following if-clauses applies to say 10, the program will exit silently without tellig the user something went wrong.

Looking further, there seems to be a whole lot of redundant code in your class-specific checks (if student_class == 7: and so on). It is good practice to put such code into a function and pass e.g. the filename as argument. I will leave that as an exercise to you, but I'm willing to help if you face serious troubles doing that.

The variable Highscore should be all lowercase as well. You may have a look at PEP8 for Python's naming recommendations. To easily check for such common mistakes, I personally would recommend to have a look at Pylint which will do such basic checks for you automatically. But it is not perfect and may lead to confusion if you are a real beginner.

Please consider replacing the calls to open the csv-files (f = open('classX.csv')) with with-statements too. The example section in the csv-module shows you exactly how to do it.