是或否输出 Python

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17953940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 09:34:24  来源:igfitidea点击:

Yes or No output Python

pythonvariables

提问by Ross Hudson

Join = input('Would you like to join me?')
if Join == 'yes' or 'Yes':
  print("Great," + myName + '!')
else:
  print ("Sorry for asking...")

So this is my code. It's longer; just including the problem. I'm asking a yes or no question and when in the console it runs smoothly until you get to it. Whatever you type you get the 'yes' output. Could someone please help? I've used elif statements as well but no luck.

所以这是我的代码。时间更长了;只是包括问题。我在问一个是或否的问题,当它在控制台中运行时,它会顺利运行,直到你得到它。无论你输入什么,你都会得到“是”的输出。有人可以帮忙吗?我也使用过 elif 语句,但没有运气。

采纳答案by kindall

if Join == 'yes' or 'Yes':

This is always true. Python reads it as:

这总是正确的。Python 将其读作:

if (Join == 'yes') or 'Yes':

The second half of the or, being a non-empty string, is always true, so the whole expression is always true because anything ortrue is true.

的后半部分or是一个非空字符串,总是为真,所以整个表达式总是为真,因为任何为or真的都是真的。

You can fix this by explicitly comparing Jointo both values:

您可以通过显式比较Join这两个值来解决此问题:

if Join == 'yes' or Join == 'Yes':

But in this particular case I would suggest the best way to write it is this:

但在这种特殊情况下,我建议最好的写法是这样的:

if Join.lower() == 'yes':

This way the case of what the user enters does not matter, it is converted to lowercase and tested against a lowercase value. If you intend to use the variable Joinelsewhere it may be convenient to lowercase it when it is input instead:

这样用户输入的大小写无关紧要,它被转换为小写并针对小写值进行测试。如果您打算在Join其他地方使用该变量,则在输入时将其小写可能会很方便:

Join = input('Would you like to join me?').lower()
if Join == 'yes':   # etc.

You could also write it so that the user can enter anything that begins with yor indeed, just y:

您也可以编写它,以便用户可以输入以y或开头的任何内容,只需y

Join = input('Would you like to join me?').lower()
if Join.startswith('y'):   # etc.

回答by Crowman

That's not how the oroperator works. Replace:

这不是or运营商的工作方式。代替:

if Join == 'yes' or 'Yes':

if Join == 'yes' or 'Yes':

with:

和:

if Join in ['yes', 'Yes']:

if Join in ['yes', 'Yes']:

and it'll do what you want.

它会做你想做的。

EDIT: Or try this, for more general purpose:

编辑:或者试试这个,用于更通用的目的:

if 'yes'.startswith(Join.lower()):

if 'yes'.startswith(Join.lower()):

which ought to match 'y', 'Y', 'ye', 'Ye', 'YE', and so on.

应该匹配“y”、“Y”、“ye”、“Ye”、“YE”等。

回答by Prahalad Deshpande

The if statement should actually be:

if 语句实际上应该是:

if Join=='yes' or Join =='Yes'

The way the ifstatement is written in your code will cause code to be evaluated this way:

的方式,如果语句写在你的代码会导致代码进行评估是这样的:

(if Join == 'yes') or ('Yes'):

Note that ('Yes')is a truthy and will always evaluate to true

请注意,('Yes')是一个真值,并将始终评估为真

回答by Stephan

I answered this questionyesterday

我昨天回答了这个问题

You can use .lower()

您可以使用 .lower()

Join = input('Would you like to join me?')
if Join.lower() == 'yes':
  print("Great," + myName + '!')
else:
  print ("Sorry for asking...")