Python 3.x Python 3 if elif else语句不是';行不通
在运行代码后,如果我输入的不是john或johnny,它仍然会打印出johnny!!!为什么会这样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
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'):
非常感谢,我真的很感激!!!