Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Python 3 if elif else语句不是';行不通_Python 3.x_If Statement - Fatal编程技术网

Python 3.x Python 3 if elif else语句不是';行不通

Python 3.x Python 3 if elif else语句不是';行不通,python-3.x,if-statement,Python 3.x,If Statement,在运行代码后,如果我输入的不是john或johnny,它仍然会打印出johnny!!!为什么会这样 user_name = input('What is your name?') if user_name.lower() == 'john' or 'johnny': print ('JOHNNY!!!') elif user_name.lower() == 'bill' or 'billy': print ('BILLY!!!') else: print ('Hello

在运行代码后,如果我输入的不是john或johnny,它仍然会打印出johnny!!!为什么会这样

user_name = input('What is your name?')

if user_name.lower() == 'john' or 'johnny':
    print ('JOHNNY!!!')
elif user_name.lower() == 'bill' or 'billy':
    print ('BILLY!!!')
else:
    print ('Hello {0}'.format(user_name))

您需要按照以下方式纠正所有情况:

if user_name.lower() == 'john' or user_name.lower() =='johnny':
一个好方法是

if user_name.lower() in {'john' ,'johnny'}:

@JacobIRR有一个更好的解决方案,但你也可以做下面发布的事情

user_name = input('What is your name?')

if user_name.lower() == 'john' or user_name.lower() == 'johnny':
    print ('JOHNNY!!!')

elif user_name.lower() == 'bill' or user_name.lower() == 'billy':
    print ('BILLY!!!')

else:
    print ('Hello {0}'.format(user_name))

user\u name.lower()在('john'或'johnny')
elif user\u name.lower()在('bill'或'billy'):
非常感谢,我真的很感激!!!