输入" Python Programming for Absolute Beginner,Third Edition"的第5章中的代码,这是我的初学者编程书。该程序似乎很自我解释......
Eq
..." while"开头的循环似乎无限运行。
Python Shell:
scores = []
choice = None
while choice != "0":
print(
"""
High Scores 2.0
0 - Quit
1 - List Scores
2 - Add a Score
"""
)
choice = input("choice: ")
print()
# exit
if choice == 0:
print("Goodbye")
# display high-score table
elif choice == 1:
print("High Scores\n")
print("NAME\tSCORE")
for entry in scores:
score, name = entry
print(name, "\t", score)
# add a score
elif choice == 2:
name = input("Player's name:")
score = int(input("What was the score:"))
entry = (name, score)
scores.append(entry)
scores.sort(reverse=true)
scores = scores[:5]
else:
print("Sorry, but", choice, "isn't a valid choice.")
input("")
尽管我在程序中定义了它,但它似乎甚至将我的输入列为无效。有人可以帮助找到错误,并指出它导致程序行为不端的原因吗?
编辑:我现在知道原始代码出了什么问题。我忘了添加" int"在我添加"输入"之前,此行上的功能(愚蠢的我)。 "重复" python2线程有帮助。
High Scores 2.0
0 - Quit
1 - List Scores
2 - Add a Score
choice: 1
Sorry, but 1 isn't a valid choice.
High Scores 2.0
0 - Quit
1 - List Scores
2 - Add a Score
choice: 2
Sorry, but 2 isn't a valid choice.
High Scores 2.0
0 - Quit
1 - List Scores
2 - Add a Score
choice: 0
Sorry, but 0 isn't a valid choice.