以下是我的任务。我被困在如何在字典中包含总数。我甚至不确定这是否可能,但我需要它来做平均。我希望推动正确的方向。 :)来自文本文件的Python保龄球程序字典
作业:编写一个程序,可以从名为“bowlingscores.txt”的外部文件中读取未知数量的保龄球及其保龄球分数(可能值为1至300)。该文件将类似于以下内容:
David
102
Hector
300
Mary
195
Jane
160
Sam
210
输出的保龄球的名字名为‘bowlingaverages.txt’外部数据文件。输入“perfect” 对于那些高于平均分数的分数,输出“高于平均水平” 对于低于平均水平的输出,输出“低于平均水平”
scores = {}
total = 0
def bowl_info(filename):
infile = open("bowlingscores.txt", "r")
for line in infile:
if line.strip().isdigit():
score = int(line)
scores[name] = score
total += score
else:
name = line.strip()
return scores
bowl_info("bowlingscores.txt")
numbowlers = len(scores)
total = 0
average = total/numbowlers
我尝试过,但得到了KeyError:'total'。 :( – Holly 2014-12-02 06:10:50