【发布时间】:2013-12-02 18:00:35
【问题描述】:
我正在尝试创建一个高分表,以相反的顺序列出游戏的高分,并保留前十名。我将它们定义为 2 个函数
我有 2 个问题,
1 - 每次我尝试运行 display_scores 部分时都会收到此错误:
'function' object is not iterable
2 - 当我添加多个分数时,每个分数都会被替换而不是添加到列表中。例如:得分为 8 的 Jim 将在添加另一个分数时被替换。
我可能在这里做一些愚蠢的事情,但我还在学习!
我的代码:
def display_scores():
print("\n High Scores\n")
print("Name", "\t\tScore")
print("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯")
for entry in scores:
name, score = entry
print(name, "\t\t", score)
def scores():
"""The High Scores Table."""
scores = []
name = input("What is your name?")
score = score_counter
entry = (name, score)
scores.append(entry)
scores.sort(reverse=True)
scores = scores[:10]
print("\n High Scores\n")
print("Name", "\t\tScore")
print("¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯")
print(name, "\t\t", score)
【问题讨论】:
-
修正缩进。如果缩进被破坏,我们无法判断您的代码的结构。
-
分数方法和分数数组的名称相同
标签: list function object iterable defined