【发布时间】:2020-02-26 21:11:25
【问题描述】:
所以我有这个任务,我必须从文本文件中计算玩家的分数,然后打印分数。如何拆分名称和分数并计算特定名称的分数?并且名称必须按字母顺序排列。
我试过这个:
file = input("Enter the name of the score file: ")
print("Contestant score:")
open_file = open(file, "r")
list = open_file.readlines()
open_file.close()
for row in sorted(list):
row = row.rstrip()
contestant = row.split(" ")
if contestant[0] == contestant[0]:
total = int(contestant[1]) + int(contestant[1])
print(contestant[0], total)
print(row)
文本文件示例:
sophia 2
sophia 3
peter 7
matt 10
james 3
peter 5
sophia 5
matt 9
程序应该是这样的:
james 3
matt 19
peter 12
sophia 10
而我目前的输出是:
sophia 10
sophia 5
【问题讨论】:
-
您当前的代码提供了什么结果?请列出您当前的输出以及您的预期输出
标签: python python-3.x list file