Python GCSE A453编程任务3的困难
在过去的几天里,我一直在尝试为A453计算部分的任务3编写一个解决方案。本质上,我的目标是制作一个程序,从参加过数学测验的班级成员那里获取数据(该测验包含三个文本文件,因为考试规范规定假设测验的参与者必须来自“1班”、“2班”或“3班”),然后根据用户的输入对其进行排序,通过以下两种方式之一:Python GCSE A453编程任务3的困难,python,sorting,Python,Sorting,在过去的几天里,我一直在尝试为A453计算部分的任务3编写一个解决方案。本质上,我的目标是制作一个程序,从参加过数学测验的班级成员那里获取数据(该测验包含三个文本文件,因为考试规范规定假设测验的参与者必须来自“1班”、“2班”或“3班”),然后根据用户的输入对其进行排序,通过以下两种方式之一: 按字母顺序排列,每个学生(在适当的班级)的考试最高分数 根据该班每个学生的最高分数,从高到低 以下是我到目前为止编写的代码: def highest_score_first(): names_
- 按字母顺序排列,每个学生(在适当的班级)的考试最高分数
- 根据该班每个学生的最高分数,从高到低
def highest_score_first():
names_with_scores_list = []
ScoresFirstList = []
AlreadySeenList = []
for line in file:
names_with_scores_list.append(line.strip())
for row in names_with_scores_list:
nameString = row[0: row.index(', ') ]
scoreString = row[row.index(', ')+2 : len(row)]
scoresFirstString = scoreString+","+nameString
#print(scoresFirstString)
ScoresFirstList.append(scoresFirstString)
ScoresFirstList.sort(reverse = True)
for row in ScoresFirstList:
nameString = row[row.index(',') : len(row)]
if nameString not in AlreadySeenList:
print(row)
AlreadySeenList.append(nameString)
def alphabetical():
names_with_scores_list = []
NamesFirstList = []
AlreadySeenList = []
for line in file:
names_with_scores_list.append(line.strip())
for row in names_with_scores_list:
nameString = row[0: row.index(', ') ]
scoreString = row[row.index(', ')+2 : len(row)]
NamesFirstString = nameString+","+scoreString
NamesFirstList.append(NamesFirstString)
NamesFirstList.sort()
for row in NamesFirstList:
nameString = row[0: row.index(',') ]
if nameString not in AlreadySeenList:
print (row)
AlreadySeenList.append(nameString)
# main code here
chosen_class = input("Which class would you like to view - one, two or three?")
if chosen_class == "one":
file = open("classonescore.txt","r")
elif chosen_class == "two":
file = open("classtwoscore.txt","r")
elif chosen_class == "three":
file = open("classthreescore.txt","r")
else:
print("Unfortunately, you have entered an invalid class name. ")
function = input("How would you like to sort the data - alphabetically or by highest score? Choose A or H")
if function.upper() == "H":
highest_score_first()
elif function.upper() == "A":
alphabetical()
从本质上讲,代码的问题在于,当用户希望一个类的数据按字母顺序排序(根据最高分数)时,只生成每个学生的最低分数以及他的名字
例如,当类1中的数据写入文本文件时,如下所示:
克里斯,08岁
克莱夫,09岁
威尔,04岁
哈里,10岁
艾哈迈德,08岁
杰夫,06岁
艾米,04岁
文努,10岁
文努,07
艾米,06岁
艾哈迈德,09岁
杰夫,04岁
哈里,07岁
威尔,06岁
克莱夫,10岁
克里斯,10岁
运行程序时(当用户希望使用“字母顺序”生成分数时)显示为:
艾哈迈德,08岁
艾米,04岁
克里斯,08岁
克莱夫,09岁
杰夫,04岁
哈里,07岁
文努,07
威尔,04岁
理想情况下,应按如下方式生成:
艾哈迈德,09岁
艾米,06岁
克里斯,10岁
克莱夫,10岁
杰夫,06岁
哈里,10岁
文努,10岁
威尔,06岁
不幸的是,我无法联系我的老师,所以任何回复都将不胜感激
非常感谢,,
Chris如果您将组合的姓名和分数列表拆分为[姓名,分数]对,可能会有所帮助。然后你可以按分数排序。或者,使用在
highest_score
函数中生成的“score name”列表进行排序,丢弃低分数,然后将行重新组合为“name score”并打印。如果将组合的名称和分数列表拆分为成对的[name,score],可能会有所帮助。然后你可以按分数排序。或者,使用您在最高分数
函数中生成的“分数名称”列表进行排序,丢弃低分数,然后将行重新组合为“名称分数”并打印。