import randomimport operatorimport pickleclassname = while True:    classpick = input(Enter class name (\A,B,C\) ).upper()    try:        if classpick in [A,B,C]:            classname = class+classpick+.txt            break        else:            raise ValueError    except ValueError:        print(Invalid value. Please enter a letter A,B or C.)        breakwith open(classname + .txt, rb) as handle:    data = pickle.load(handle)def test():    num1 = random.randint(1, 10)    num2 = random.randint(1, num1)    ops = {        '+': operator.add,        '-': operator.sub,        '*': operator.mul,    }    ops_keys = list(ops.keys()) ##=> ['+', '*', '-']    ops_key_choice = random.choice(ops_keys)  #e.g. '+'     operation = ops[ops_key_choice]  #e.g. operator.add    correct_result = operation(num1, num2)    question = (Q{}. What is {} {} {}?.format(i+1,num1, ops_key_choice, num2))    print(question)    while True:        try:            user_answer= int(input(Answer: ))        except ValueError:            print (That is not a valid answer)            print (question)        else:            break    if user_answer != correct_result:        print (Incorrect. The right answer is {}.format(correct_result))        return False    else:        print(Correct!)        return True###def sort_dict(data):    while True:        sortpick = str(input('''Enter a number to sort by:                         \n1. Alphabetically                         \n2. Highest Score                         \n3. Highest Average Score'''))        try:            if sortpick in [1,2,3]:                break            else:                raise ValueError        except ValueError:            print(Invalid value. Please enter a number in the range of 1-3.)        break    if sortpick == 1:    #Alphabetical        for x, y in sorted(data.items()):            print(x,y)    elif sortpick == 2:    #Highest Score        for k,v in sorted(((max(data[k]), k) for k in data), reverse=True):             print('{} : {}'.format(v,k))    else:   #Highest Average Score        sort = sorted(data, key=lambda k: sum(data[k]) / len(data[k]), reverse=True)        for i in sort:            print({} : {}.format(i,                                   round(sum(data[i])/len(data[i]),1 ) ))username = input(What is your name? ).upper()print(Hi {}! Welcome to the Primary School Arithmetic quiz....format(username))correct_count = 0question_count = 10for i in range(question_count):    if test():        correct_count +=1if username in data:    if len(data[username]) == 3:        del(data[username][0])        data[username].append(correct_count)    else:        data[username].append(correct_count)else:    data[username] = [correct_count]print({}: You got {}/{} questions correct. {}.format(    username,     correct_count,     question_count,    'Great job!' if (correct_count>7) else 'Better luck next time...'))with open(classname+.txt, wb) as handle:    pickle.dump(data, handle)print({}: {}.format(k, v) for k, v in data.items())    #prints whole dictfor k, v in data.items():    print({}: {}.format(k, v))while True:    try:        sort_or_not = input(Would you like to sort the class results?? Y/N).upper()        if sort_or_not.isalpha() == True:            break        else:            raise ValueError    except ValueError:        print (That is not a valid input)    breakif sort_or_not == Y:    print()else:    quit()sort_dict(data)The code above is designed to ask users a set of 10 randomized maths questions then save the score to a file and sort the scores.How can I make this code more efficient?
Random maths quiz with sorting
python;python 3.x;sorting;quiz