Help

Peter Otten __peter__ at web.de
Fri Jul 26 01:27:55 EDT 2013


tyler at familyrobbins.com wrote:

> I'm a bit new to python and I'm trying to create a simple program which
> adds words and definitions to a list, and then calls them forward when
> asked to.

> while escape < 1:
> 
>     choice = input("Type 'Entry' to add a word to the Dictionary, 'Search'
>     to find a word, and 'Exit' to quit. ")
> 
>     if choice == '`':
>         break
>     
>     if choice == 'Entry' or 'entry':

> However, if I run the program using anything but 'entry', the program
> still runs the 'entry' subroutine. After the 'entry' subroutine is run
> once, no options work. Ex:

The expression 

choice == 'Entry' or 'entry'

is evaluated by Python as

(choice == 'Entry') or 'entry'

All strings but "" are True in a boolean context, so this has the same 
effect as

(choice == 'Entry') or True

and is always True. Possible fixes:

if choice == "Entry" or choice == "entry":
if choice in ("Entry", "entry"):
if choice.casefold() == "entry".casefold(): # will accept ENTRY, eNtRy etc.




More information about the Python-list mailing list