Также с моим усреднением он несколько раз печатает имя слишком много раз. У меня есть этот код, но борюсь с указанием оценки низкого до самого высокого в моем текстовый файл, он выглядит следующим образом:
имя, 1
имя, 4
имя, 7
имя, 9
имя, 10Мне нужен код, который позволяет мне найти наивысший балл в моем .txt doc
Это мой код:
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, 4,5,6
и только три последние показаны оценки. Для самого высокого до самого низкого балла я бы ожидать такой результат: имени, 10 2name, 8 5name, 4 3name, 5
Благодаря
Не могли бы вы показать нам, что выход вы получаете и то, что выход вы exepct? Это поможет нам лучше понять ваш вопрос. – Brobin
Только что сделал это сейчас. спросите, хотите ли вы что-то еще – BeggineratPython
Все ли экземпляры 'name' того же человека? – Brobin