0

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: "))
  • The duplicate link says that this code is for python2 and you are running python3. – Alper Apr 07 '18 at 20:10
  • 2
    you need to properly format your question- see https://stackoverflow.com/editing-help (your python code indentation is wrong, and this is why random parts of your question are shaded) – avigil Apr 07 '18 at 20:11
  • As a new user, you probably should have read that introductory [tour] – don't worry, it's real short, but it familiarizes you with the basics. Also, check what the Help buttons can offer while you are entering your questions (per @avigil's comment). – Jongware Apr 07 '18 at 20:14
  • @Alper Yeah, but they did provide my solution – NunyaBuisiness Apr 07 '18 at 20:50

0 Answers0