如果有很多或者在承包形式的Python [英] Python if with many or in a contracted form
本文介绍了如果有很多或者在承包形式的Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在学习Python和我发现自己失去了试图创建一个if语句应该是真,如果用户输入y或肯定。
I'm learning python and I found myself lost trying to create a an if statement that should be true if the user input y or yes.
#!/usr/bin/env python3
user_input = input('Would you like to go on?')
lowui = user_input.lower
if lowui == ('y' or 'yes'):
print('You want to go on')
else
print('See you later, bye')
问题是,它成为真正只有当我键入y但不是肯定的。如果删除了括号就变成始终为false。好吧,我可以不喜欢
The problem is that it becomes true only if I type y but not for yes. If I remove the parenthesis it becomes always false. Ok, I can do a workaround like
if lowui == 'y' or lowui == 'yes':
但我想知道是否有不逼我写了这么多次塔变量的任何伎俩。结果
谢谢你在前进。
but I was wondering if there is any trick that don't force me to write so many times tha variable.
Thank you in advance.
推荐答案
将其更改为
if lowui in ('y', 'yes'):
另外这是错误的:
Also this is wrong:
lowui = user_input.lower
应该是:
lowui = user_input.lower() # Actually call the lower function
这篇关于如果有很多或者在承包形式的Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文