seeaemearohin
seeaemearohin

Reputation: 81

Python Choice Not In List

while True:

print "Please choose from one of the five following options:"

print " 1. 10^1\n 2. 10^2\n 3. 10^3\n 4. 10^4\n 5. 10^5\n"
    choice = raw_input()

    if choice == 1:
        print "1"
    elif choice == 2:
        print "2"
    elif choice == 3:
        print "3"
    elif choice == 4:
        print "4"
    elif choice == 5:
        print "5"

    while choice not in [1,2,3,4,5]:
        print "Not a valid choice!\n"
        break

What syntax should I be using? I keep getting not a valid choice. Its like Python is placing every choice even 1,2,3,4,5 in the list of points outside of that list.

Upvotes: 1

Views: 1458

Answers (3)

Padraic Cunningham
Padraic Cunningham

Reputation: 180401

I would use a dict, check if the key/choice is in the dict, if it is print the choice and break else print the "Not a valid choice!\n" message:

# map choice to whatever you want to print

d = {"1": "1", "2": "2", "3": "3", "4": "4", "5": "5"}
while True:
    choice = raw_input("Please choose from one of the five following options:\n"
                       "1. 10^1\n 2. 10^2\n 3. 10^3\n 4. 10^4\n 5. 10^5\n")
    # if the key is in the dict, the choice is valid
    # so print the choice value and break.
    if choice in d:
        print(d[choice])
        break
    # if we get here choice was invalid
    print "Not a valid choice!\n"

There would be no point converting choice to an int, leave it as a string and do the lookup on the dict also using strings as keys. You can store whatever value you want for each choice as a value in the dict.

Upvotes: 1

Intrepid Traveller
Intrepid Traveller

Reputation: 95

while True:

   print "Please choose from one of the five following options:"

   print " 1. 10^1\n 2. 10^2\n 3. 10^3\n 4. 10^4\n 5. 10^5\n"
choice = raw_input()

if choice == 1:
    print "1"
elif choice == 2:
    print "2"
elif choice == 3:
    print "3"
elif choice == 4:
    print "4"
elif choice == 5:
    print "5"

else:
    print "Not a valid choice!\n"

this should be the easiest fix

Upvotes: -1

EdChum
EdChum

Reputation: 394021

You need to cast the raw_input to an int:

choice = int(raw_input())

the type of choice from raw_input will be a str which is why there are no matches, so you need to either change all your if to compare with the character or convert choice to an int

EDIT

In light of the comments, it would be better to change your comparison's to compare against the single character string, for one this is what you're intending anyway, and secondly it protects against invalid conversions for instance if you enter an input value that cannot be cast to an int then this will throw an exception so I'd modify your code to this:

choice = raw_input()

if choice == "1":
    print "1"
elif choice == "2":
    print "2"
elif choice == "3":
    print "3"
elif choice == "4":
    print "4"
elif choice == "5":
    print "5"

while choice not in ["1","2","3","4","5"]:
    print "Not a valid choice!\n"
    break

Upvotes: 3

Related Questions