3

starting out learning Python so please bear with me if this question seems very obvious. I am trying to create a high scores program in which the program would use list methods to create and mantain a list of ausers best scores for a computer game. What is happening however is that although I have code in place depending on user input, the while loop keeps executing and ignoring user input. Please have a look at code below, would love answers on what I am doing wrong. Thanks in advance.

scores =[]
choice = None

while choice != "0":
    print """"High Scores Keeper
    0 - Exit
    1 -Show Scores
    2 - Add A score
    3- Delete a score.
    4- Sort Scores"""
    choice = raw_input("Choice:")
    print 


    if choice == "0":
        print "Good Bye"

    elif choice == "1":
        print "High Scores"
        for score in scores:
            print score

    elif choice == "2":
        score = int(raw_input("What score did you get?:  "))
        scores.append(score) 

When I execute the loop and I choose 1 for example, rather than printing High Scores, the loop just goes on again and its the same for two. Please help.

Leigh
  • 12,038
  • 4
  • 28
  • 36
TKA
  • 51
  • 2

2 Answers2

1

You coded your loop so that it will keep going on while choice != "0" and will only get out of the loop if choice == "0". If you want to break out of the loop with "1" you need a loop condition that corresponds to that:

while choice != "0" and chioce != "1" and choice != "2" and ...

Or you can write it in a more succinct manner:

while 0 <= int(choice) and int(choice) <= 4:

while choice not in ["0", "1", "2", "3", "4", "5"]:

#or something like that.
hugomg
  • 68,213
  • 24
  • 160
  • 246
  • I am not sure I follow, so for example suppose the user put in "1" and then what was then printed was the high scores which then took the most previous scores put into the program (assuming that someone had put in a score already), would it then prin the High Scores Keeper string again? – TKA Nov 10 '13 at 02:02
1
scores =[]
choice = None

while choice != "0":
    print """High Scores Keeper
    0- Exit
    1- Show Scores
    2- Add A score
    3- Delete a score.
    4- Sort Scores"""
    choice = raw_input("Choice:")
    if choice == "0":
        print "Good Bye"
    elif choice == "1":
        print "High Scores"
        for score in scores:
            print score
    elif choice == "2":
        score = int(raw_input("What score did you get?:  "))
        scores.append(score)
Aladdin
  • 384
  • 1
  • 5