我正在尝试创建一个高分表,以相反的顺序列出游戏的高分,并保持前十分。我将它们定义为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)
答案 0 :(得分: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)