case insensitive - Python string.lower() not working? -
i trying create case-insensitive user input choice. think should correct isn't working. relevant snippet of code:
while true: search = raw_input("choose search or b: ") search = search.lower() if search != {'a','b'}: print "that not valid choice." else: if search == 'a': searchafunction() if search == 'b': searchbfunction() else: print "search again." i want user able input 'a' or 'a'. @ moment working solution have this. doesn't seem pythonic?:
while true: search = raw_input("choose search or b: ") if search != {'a','a','b','b'}: print "that not valid choice." else: if search in {'a','a'}: searchafunction() if search in {'b','b'}: searchbfunction() else: print "search again." when include search = search.lower() stuck in loop of "search again". (in full program allows user choose search again or b after completing search). ideas?
two issues:
1) instead of
if search != {'a','b'}: ... use
if search not in {'a', 'b'}: the != operator comparison, not set membership.
2) use search.upper() instead of search.lower() (or alternatively, use 'a' instead of 'a' in rest of code ...)
Comments
Post a Comment