import operator
import csv
question= input("Are you a student or teacher: ")
if question=="student" or "s" or "Student":
print("You are using the wrong program. Please use the arithmetic quiz")
elif question=="teacher" or "t" or "Teacher":
print("Hello and welcome to the program which lets you see your students' scores in the arithmetic quizes")
form=input("What form do you want to view the results of? 1,2 or 3?")
if form=="1":
print("if you want the data sorted by names in alphabetical order")
print("enter 1, if you want the data sorted by scores highest to")
print("lowest enter 2, if you want the data sorted by average score")
teachermenu=input("highest to lowest enter 3:")
if teachermenu=="1":
start=csv.reader(open("form1.csv"), delimiter=";")
datasorted= sorted(start, key=operator.itemgetter(0))
data= open("form1.csv","r")
for eachline in datasorted:
eachline.sort()
print(eachline)
data.close()
elif teachermenu=="2":
data= open("form1.csv")
start=csv.reader(data)
for line in start:
scores = (line[1:5])
scores.sort(reverse = True)
print(scores)
elif teachermenu=="3":
studentsaverages=[]
data= open("form1.csv")
start=csv.reader(data)
for line in data:
numbers=(line[2:4])
numbers=list(map(int, numbers))
mean= round(sum(numbers)/len(numbers))
data.append(mean)
data.sort(reverse=True)
print(studentsaverages)
在这段代码中,平均值的工作不起作用,我不知道为什么 我试了好几次 但似乎没有用 代码的其他部分也起作用 但是工作出来的意思并不重要 例如把它放到最低的作品 将名称按字母顺序排列
答案 0 :(得分:0)
如果我理解你正在尝试做什么,你需要追加然后将平均值分类到 studentsaverages 变量(在for循环之后)而不是数据。 例如:
elif teachermenu=="3":
studentsaverages=[]
data = open("form1.csv")
start=csv.reader(data)
for line in data:
numbers=(line[2:4])
numbers=list(map(int, numbers))
mean = round(sum(numbers)/len(numbers))
studentsaverages.append(mean)
studentsaverages.sort(reverse=True)
print(studentsaverages)