這是我的程序。除了我需要放置投球手名稱,分數和標題(平均,完美,低於平均水平,高於平均水平)的部分以外,一切都可以正常工作。如何確保所有部件進入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