Maths quiz that read/writes CSV files Could this code possibly be shortened, or even better be rewritten in the fewest lines of code possible?

import csv

import random
import math
import operator as op



name=input("What is your name: ")
print("Hello there",name,"Welcome to the math quiz, you will be asked 10 random questions on Addition, Multiplication and Subtraction")





choice = random.choice(['x','-','+'])
finish = False
questionnumber = 0
correctquestions = 0
Class = "" 


while finish == False:
       choice = random.choice(['x','-','+'])
       if questionnumber &lt; 10 | questionnumber &gt;= 0:
           number1 = random.randrange(1,10)
           number2 = random.randrange(1,10)
           print((number1),(choice),(number2))
           answer=int(input("What is the answer?"))
           questionnumber = questionnumber + 1

        if choice==("+"):
            realanswer = number1+number2
            if answer==realanswer:
                print("Well done that's the correct answer")
                correctquestions = correctquestions + 1
            else:
                print("Unlucky wrong answer")

        if choice==("x"):
            realanswer = number1*number2
            if answer==realanswer:
                print("That's the correct answer")
                correctquestions = correctquestions + 1
            else:
                print("That was not correct")

        elif choice==("-"):
            realanswer = number1-number2
            if answer==realanswer:
                print("That's the correct answer")
                correctquestions = correctquestions + 1
            else:
                print("Unlucky not the corret")

    else:
        finish = True
        print("Well done You have completed the math quiz, Your score was " + str(correctquestions) + "/10 questions.")

while Class not in ("1" , "2" , "3"):
    Class = input("whish class are you in?")

ScoresList = []

if Class == "1":
    with open("class1.csv","r") as class1:
        reader = csv.reader(class1)
        for row in reader:
            ScoresList.append(row)
elif Class == "2":
    with open("class2.csv" , "r") as class2:
        reader = csv.reader(class2)
        for row in reader:
            ScoresList.append(row)

elif Class == "3":
    with open("class3.csv","r") as class3:
        reader = csv.reader(class3)
        for row in reader:
            ScoresList.append(row)
else:
    print("Please enter a valid class")


position = 0 
match = 0


for row in ScoresList:

    if len(row) &gt; 0 and row[0] == name: 
        match = 1
        break
    else:
        position += 1


print(position , match)
if match == 0:
    ScoresList.append([name,str(correctquestions)])
else:

    row = ScoresList[position] 
    if len(row) == 4:  
        del row[1]  
    row.append(str(correctquestions))  

for row in ScoresList:
    print(row)

if Class == "1":
    with open ("class1.csv" , "w", newline='') as file:
        writer = csv.writer(file)
        for row in ScoresList:
            writer.writerow(row)

if Class == "2":
    with open ("class2.csv" , "w", newline='') as file:
        writer = csv.writer(file)
        for row in ScoresList:
            writer.writerow(row)


if Class == "3":
    with open ("class3.csv" , "w", newline='') as file:
        writer = csv.writer(file)
        for row in ScoresList:
            writer.writerow(row) `
You surely can shorten it by also improving readability

For example:

if Class == "1":
    with open ("class1.csv" , "w", newline='') as file:
        writer = csv.writer(file)
        for row in ScoresList:
            writer.writerow(row)

if Class == "2":
    with open ("class2.csv" , "w", newline='') as file:
        writer = csv.writer(file)
        for row in ScoresList:
            writer.writerow(row)


if Class == "3":
    with open ("class3.csv" , "w", newline='') as file:
        writer = csv.writer(file)
        for row in ScoresList:
            writer.writerow(row) `  


Becomes:

if Class in ("1", "2", "3"):
    with open ("class{}.csv".format(Class) , "w", newline='') as file:
        writer = csv.writer(file)
        for row in ScoresList:
            writer.writerow(row) `  


And, as you do that twice, you can extract it into a function to further avoid duplication.



You can also extract common code out of conditional branches

        if answer==realanswer:
            print("That's the correct answer")
            correctquestions = correctquestions + 1
        else:
            print("Unlucky not the corret")


is repeated in all of the ifs so you can write it just once at the bottom.