Python 如何在函数中对项目进行排序?

Python 如何在函数中对项目进行排序?,python,function,file,Python,Function,File,对于我的计算机课程,我必须创建一个算术测验,将用户的分数保存在一个文件中。我已经完成了这部分,但对于下一个任务,我必须按照字母顺序打印用户名,并显示其最高三分。然后打印从高到低的分数(显示哪些用户得到了哪个分数),然后找到每个用户的平均分数,并按从高到低的顺序打印平均分数(再次显示哪个用户得到了哪个分数)。不幸的是,我不知道怎么做。请帮帮我。到目前为止,我的代码是: import random from random import randint import math import filei

对于我的计算机课程,我必须创建一个算术测验,将用户的分数保存在一个文件中。我已经完成了这部分,但对于下一个任务,我必须按照字母顺序打印用户名,并显示其最高三分。然后打印从高到低的分数(显示哪些用户得到了哪个分数),然后找到每个用户的平均分数,并按从高到低的顺序打印平均分数(再次显示哪个用户得到了哪个分数)。不幸的是,我不知道怎么做。请帮帮我。到目前为止,我的代码是:

import random
from random import randint
import math
import fileinput

question = 0
ans = 0
score = 0

firstname = input("What is your first name?")
lastname = input("What is your last name?")
classofstudent = input("Which class are you in? (1,2 or 3?)")
print ("Welcome to this arithmetic quiz")
print ("")
for question in range(10):
    def multiplication():
        global ans
        numberOne, numberTwo = random.randint(0,10), random.randint(0,10)
        print ("What is", numberOne, "*", numberTwo)
        ans = (numberOne * numberTwo)

    def subtraction():
        global ans
        numberOne, numberTwo = random.randint(0,10), random.randint(0,10)
        print ("What is", numberOne, "-", numberTwo)
        ans = (numberOne - numberTwo)

    def addition():
        global ans
        numberOne, numberTwo = random.randint(0,10), random.randint(0,10)
        print ("What is", numberOne, "+", numberTwo)
        ans = (numberOne + numberTwo)

    operation = [multiplication, subtraction, addition]
    randoperation = random.choice(operation)
print()
def main():
    question = 0
    score = 0
    randoperation = random.choice(operation)

    whileTrue:
        try:
            randoperation()
            randoperation = random.choice(operation)
            if question >= 10:
                break

            userinput = int("What is your answer?")
            if userinput = ans:
                global score
                score = score + 1
                global question
                question += 1
                print ("Your answer is correct")
            else:
                print ("Incorrect")
                question += 1
        except ValueError:
            print ("Invalid answer")
            question += 1
        while question == 10:
            print (firstname ,lastname ,"you scored" , score,"/10")
            break
def filewrite():
    fileopen = open("data.txt", "a")
    counts = str(score)
    cos = str(classofstudent)
    Textline = (firstname + " " + lastname + " / class " + cos + " /score = " + counts + "/10" + "\n")
    fileopen.write(Textline)
    fileopen.close

def read():
    with open ('data.txt') as data:
        print ("")
        for line in data:
            print (line, end ='')
        print ("")
    check = "Y"

main() 
filewrite()
read()

您可以先将结果读入列表或字典,然后使用
sorted()
函数按字母顺序、升序或降序对元素进行排序

>>> elements = [22, 333, 0, -22, 1000]
>>> print sorted(elements)
[-22, 0, 22, 333, 1000]
>>> 
>>> 
>>> for element in reversed(sorted(elements)):
...     print element
... 
1000
333
22
0
-22 
请查看本教程