After typing out a code from chapter 5 of "Python Programming for the Absolute Beginner, Third Edition", a beginner programming book of mine. The program seems pretty self explanatory...
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("")
...the "while" loop at the beginning appears to run infinitely.
Python Shell:
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.
It even seems to list my input as invalid, despite me defining it in the program. Can someone help find the error, and point out why it's causing the program to misbehave?
EDIT: I now know what was wrong with the original code. I forgot to add an "int" function on this line, before I added "input" (stupid me). The "duplicate" python2 threads helped.
choice = int(input("choice: "))