[英]how to give different outputs depends on input in python?
im trying to give different outputs depends on inputs, but i will get the same output for all inputs. 我试图给不同的输出取决于输入,但我将为所有输入得到相同的输出。
i tried to remove the "or" operator, and the code will work fine! 我试图删除“或”运算符,该代码将正常工作! but when using the "or" operator again, the code wont work the way it should 但是当再次使用“或”运算符时,代码将无法正常工作
voroodi = input('Enter Something: ')
if voroodi == 'h' or 'H':
print('hello')
elif voroodi == 'g' or 'G':
print('Goodbye')
i expect to get output of "Hello" when i enter the "H" or "h" as input and "Goodbye" for "G" "g". 当我输入“ H”或“ h”作为输入,并输入“ G”“ g”的“再见”时,我希望得到“ Hello”的输出。
but im just getting hello for any input that i enter! 但是我只是为我输入的任何内容打招呼!
or
doesn't work that way. or
那样行不通。 You must say; 你必须说;
if voroodi == 'h' or vortoodi == 'H':
'H'
all by itself is considered True
. 'H'
本身就被认为是True
。 Any non-empty string is considered true, so your original statement is if voroodi == 'h' or True:
which is always true. 任何非空字符串都被视为true,因此您的原始声明是if voroodi == 'h' or True:
始终为true。
It should be 它应该是
if voroodi == 'h' or voroodi == 'H':
print('hello')
elif voroodi == 'g' or voroodi == 'G':
print('Goodbye')
Rakesh solution will also worl\\k Rakesh解决方案也将无法正常运行
You can use the operator in
: 您可以使用运营商in
:
voroodi = input('Enter Something: ')
if voroodi in 'hH':
print('hello')
elif voroodi in 'gG':
print('Goodbye')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.