Python - 'function'对象不能为高分表迭代

时间:2013-11-19 09:04:07

标签: list function object iterable defined

我正在尝试创建一个高分表,以相反的顺序列出游戏的高分,并保持前十分。我将它们定义为2个函数

我有2个问题,

1 - 每当我尝试运行display_scores部分时,我都会收到此错误:

'function' object is not iterable

2 - 当我添加多个分数时,将替换每个分数而不是添加到列表中。例如:当得到另一个分数时,得分为8的吉姆将被替换。

我可能在这里做了些蠢事,但我还在学习!

我的代码:

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)

1 个答案:

答案 0 :(得分:2)

你必须这样做 1)更改功能的名称和 2)使分数全球化

scores = []

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 scores1():
    """The High Scores Table."""

    global 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)