Is the Python "in" operator not working correctly?

The function inin my python script works very weird. I do not know if this will be so, or if this is a bug in 3.3. Here is part of my script:

command = input("\ ")
if command in ['help', 'HELP', 'Help']:
        print (help)

The first time I tried, typing help or HELP is working until help is working.

The next time I tried Help and HELP work while help does not work.

Every time I try to do this, he will be random on what he accepts and what does not. Someone please help me if this is a bug or something is wrong with the script.

0
source share
3 answers

Where is the variable defined help(or do you want to use the built-in)? Perhaps the problem is what you wantedprint(command)

in . , .

if command.lower() == 'help':
+7

"in" , , = input (''), , , . , .

+5
command = input("\ ")
if command in ['help', 'HELP', 'Help']:
        print (help)

works for me anyway.

But I noticed that if you press Enter before entering Help, HELP or help, do not catch it. Make sure that you do not add any other characters, such as empty space or a new line. Or catch the input with a regular expression instead of a full word.

+1
source

All Articles