另外,根据我的平均值,有时候打印名称的次数太多了。
我有这个代码,但我正在努力在我的文本文件中显示从最低到最高的分数,它看起来像这样:
名,1个
名,4
名,7
名,9
名字,10
这是我的代码:
import sys
Viewclassnum= input ('Which class do you want to view 1,2 or 3?')
print('please input a, b or c')
print('a)Alphabeically')
print('b)average')
print('c)highest to lowest')
Viewclasssort= input ('how would you like to view it:')
#setting variable fd to open the file set to File_name (from when the program asked the user for
if Viewclassnum=='1' and Viewclasssort=='a':
with open('class1.txt', 'r') as r:
for line in sorted(r):
print(line, end='')
Again=input('Do you want to view another class yes or no?')
if Again=='yes':
Viewclassnum= input ('Which class do you want to view 1,2 or 3?')
print('please input a, b or c')
print('a)Alphabeically')
print('b)average')
print('c)highest to lowest')
Viewclasssort= input ('how would you like to view it:')
if Again=='no':
print('Bye')
if Viewclassnum=='1' and Viewclasssort=='b':
fd = open('class1.txt')
lines = [line.strip() for line in fd]
f = {}
for line in lines:
split = [i for i in line.split(',')]
key = split[0]
f[key] = [int(n) for n in split[1:]]
avg_mark = lambda name:sum(f[name])/len(f[name])
for i in sorted(f.keys(),key=avg_mark,reverse=True):
print (i,avg_mark(i),"\n")
Again=input('Do you want to view another class yes or no?')
if Again=='yes':
Viewclassnum= input ('Which class do you want to view 1,2 or 3?')
print('please input a, b or c')
print('a)Alphabeically')
print('b)average')
print('c)highest to lowest')
Viewclasssort= input ('how would you like to view it:')
if Again=='no':
print('Bye')
输出结果为:
Which class do you want to view 1,2 or 3?1
please input a, b or c
a)Alphabeically
b)average
c)highest to lowest
how would you like to view it:a
name,3
name,8
name,9
2name,4
2name,7
2name,8
Do you want to view another class yes or no?yes
Which class do you want to view 1,2 or 3?1
please input a, b or c
a)Alphabeically
b)average
c)highest to lowest
how would you like to view it:b
name 3.0
name 8.0
name 9.0
2name 9.0
2name 4.0
name 9.0 (repeated don't know why)
name 7.0
我还希望将所有名称组合在一起,使它看起来像这样:
名,3,5,6-
2name -1,4,5,6-
仅显示三个最新分数。 对于从最高到最低的分数,我期望这个结果: 名,10 2name,8 5name,4 3name,5
由于
答案 0 :(得分:0)
以下是我认为我会如何处理这个问题。因为没有订购字典,它们没有排序,所以我列出了可以排序的Student对象列表。
我还减少了很多重复3次的输入代码
import sys
class Student:
def __init__(self, name, marks):
self.name = name
self.marks = marks
def avg_mark(self):
return sum(self.marks)/len(self.marks)
def __str__(self):
return '{0}: {1}, {2}'.format(self.name, self.marks, self.avg_mark())
def load_grades(filename):
fd = open(filename, 'r')
f = {}
for line in fd:
split = line.split(',')
key = split[0]
marks = [int(n) for n in split[1:]]
try:
f[key] += marks
except KeyError:
f[key] = marks
students = []
for key, values in f.items():
students.append(Student(key, values[-3:]))
return students
def sort_by_name(students):
def get_name(student):
return student.name
students = sorted(students, key=get_name)
return students
def sort_by_avg(students):
def get_avg(student):
return student.avg_mark()
students = sorted(students, key=get_avg)
return students
again = True
while again:
class_num = input ('Which class do you want to view 1,2 or 3?')
print('please input a, b or c')
print('a)Alphabeically')
print('b)average')
print('c)highest to lowest')
sort = input ('how would you like to view it:')
if class_num =="1" and sort =="a":
students = load_grades('class1.txt')
students = sort_by_name(students)
elif class_num =="1" and sort =="b":
students = load_grades('class1.txt')
students = sort_by_avg(students)
for student in students:
print(student)
Again=input('Do you want to view another class yes or no?')
again = (Again=='yes')
print('bye')
示例输出:
Which class do you want to view 1,2 or 3?1
please input a, b or c
a)Alphabeically
b)average
c)highest to lowest
how would you like to view it:a
2name: [4, 7, 8, 69], 22.0
name: [3, 8, 9], 6.666666666666667
Do you want to view another class yes or no?yes
Which class do you want to view 1,2 or 3?1
please input a, b or c
a)Alphabeically
b)average
c)highest to lowest
how would you like to view it:b
name: [3, 8, 9], 6.666666666666667
2name: [4, 7, 8, 69], 22.0
Do you want to view another class yes or no?no
bye