我有一个程序,茶壶可以通过这个程序查看她学生的眩晕结果,并以一种丰富的方式对它们进行分类:
if role == 2:
class_number = prompt_int_big("Which class' scores would you like to see? Press 1 for class 1, 2 for class 2 or 3 for class 3")
filename = (str(class_number) + "txt")
sort_or_not = prompt_int_small("Would youlike to sort these scores in any way? Press 1 if the answer is no or 2 if the answer is yes")
if sort_or_not == 1:
f = open(filename, "r")
lines = [line for line in f if line.strip()]
lines.sort()
for line in lines:
print (line)
if sort_or_not == 2:
type_of_sort = prompt_int_big("How would you like to sort these scores? Press 1 for scores in alphabetical order with each student's highest score for the tests, 2 if you would like to see the students' highest scores sorted from highest to lowest and 3 if you like to see these student's average scores sorted from highest to lowest")
if type_of_sort == 1:
with open(filename , 'r') as r:
for line in sorted(r):
print(line, end='')
if type_of_sort == 2:
with open (filename,'r') as r:
def score(line):
return int(line.split(':')[1])
for line in sorted(r, key=score, reverse = True):
print(line)
if type_of_sort == 3:
with open (filename,'r') as r:
def score(line):
returnint(line.split(':')[1])
average = sum(map(int, score))/len(score)
print(name,"--",average)
但是,当选择第三个选项时会出现错误:
average = sum(map(int, score))/len(score)
TypeError-'function' object is not iterable