这是我的程序。除了我需要放置投球手名称,分数和标题(平均,完美,低于平均水平,高于平均水平)的部分以外,一切都可以正常工作。如何确保所有部件进入outfile?非常感谢!从程序中写入输出文件python
***好的,所以我得到了正确的文件输出除了没有添加标题。我需要的输出是这个样子:
Jane 160 Below Average Hector 300 PERFECT! Mary 195 Above Average Sam 210 Above Average David 102 Below Average
scores = {}
def bowl_info(filename):
infile = open("bowlingscores.txt", "r")
total = 0
for line in infile:
if line.strip().isdigit():
score = int(line)
scores[name] = score
else:
name = line.strip()
return scores
def titles():
for name, score in scores.items():
if score == 300:
print name , score, "PERFECT!"
elif score < average:
print name , score, "Below Average"
elif score > average:
print name , score, "Above Average"
else:
print name , score, "Average"
bowl_info("bowlingscores.txt")
numbowlers = len(scores)
total = sum(scores.values())
average = total/numbowlers
titles()
for items in scores.items():
outfile = open("bowlingaverages.txt", "w")
具体什么是你遇到的麻烦?您是否尝试过搜索“在Python中写入文件”? – 2014-12-03 01:21:39
我不确定如何确保名称,分数和标题(如函数中定义的)在输出文件中。我想我拥有除标题以外的一切。 – Holly 2014-12-03 02:40:57