-2

I am trying to create a case-insensitive user input choice. But what I think should be correct isn't working. This is the relevant snippet of code:

while True:

    search = raw_input("Choose search A or B: ")
    search = search.lower()

    if search != {'A','B'}:
        print "That was not a valid choice."

    else:
        if search == 'A':
            searchAfunction()
        if search == 'B':
            searchBfunction()
        else:
            print "Search again."

I want the user to be able to input 'a' or 'A'. At the moment this is the only working solution I have is this. It doesn't seem very Pythonic?:

while True:

    search = raw_input("Choose search A or B: ")

    if search != {'A','a','B','b'}:
        print "That was not a valid choice."

else:
    if search in {'A','a'}:
        searchAfunction()
    if search in {'B','b'}:
        searchBfunction()
    else:
        print "Search again."

When I include the search = search.lower() I just get stuck in a loop of "Search again". (In the full program this allows the user to choose to search again by A or B after completing a search). Any ideas?

emiden
  • 13
  • 1
  • 3

4 Answers4

3

two issues:

1) Instead of

if search != {'A','B'}:

... use

if search not in {'A', 'B'}:

The != operator is only for comparison, not for set membership.

2) use search.upper() instead of search.lower() (or alternatively, use 'a' instead of 'A' in the rest of your code ...)

Emile
  • 2,946
  • 2
  • 19
  • 22
1

You have two problems:

Firstly, you are converting the input to lower case with str.lower, but then trying to compare to upper case characters in the if statements.

Secondly, search != {'A', 'B'} should be search not in {'A', 'B'} instead.

Putting both together, you get if search not in {'a', 'b'}

rlms
  • 10,650
  • 8
  • 44
  • 61
  • Thank you so much, that makes a lot of sense. Don't know why it has taken me so long pouring over this to realise that .lower() of course needs lower case characters in the if statements! Thanks! – emiden May 11 '15 at 15:06
  • @emiden Glad to help! If one of these answers solved your problem, it would be good to accept it at some point. – rlms May 11 '15 at 19:03
1

You'll need to compare against lower case literals, and use the appropriate comment comparator for testing the contents of your interable while True:

    search = raw_input("Choose search A or B: ")
    search = search.lower()

    if not search in {'a','b'}:
         print "That was not a valid choice."

    else:
         if search == 'a':
            searchAfunction()
         if search == 'b':
            searchBfunction()
         else:
            print "Search again."
OYRM
  • 1,395
  • 10
  • 29
  • Thank you so much! I don't know why I didn't realise that .lower() meant I needed to use lower case literals, it is so obvious! – emiden May 11 '15 at 15:05
0

You have a few things wrong with your code:

  • First you change your search variable to lower case, but then do all of your comparisions to upper case letters. Of course those will fail.

Try this instead:

search = search.upper()
  • Second, your != statement is wrong. Here you are comparing if the input is not equal to {'A', 'B'}.

Instead, you want to compare that the input is part of the list:

if search not in ['A','B']:
Andy
  • 49,085
  • 60
  • 166
  • 233