【问题标题】:Python "if" condition difference between "and" & "or"Python“if”条件“and”和“or”之间的区别
【发布时间】:2019-07-23 23:58:32
【问题描述】:

我希望用户通过准确地说“是”或“否”来决定是否删除选定的文件。无论出于何种原因,“是”有效,但“否”循环返回并决定返回初始 if 语句

我已经尝试了一些方法,经过一些谷歌搜索后,我似乎无法找到适合此问题的响应。我试过了:

使用 str() 和不使用 str() 更改 inputText

我尝试过在字符串文本上使用引号和撇号。

我已尝试更改初始 If 语句:

第一:

if inputText != ('yes' or 'no'):

我什至尝试过

第二:

if inputText != 'yes' or inputText != 'no':

第三:

if (inputText != 'yes') or (inputText != 'no'):

我得到的最接近的是 First。出于某种原因,“是”似乎有效,但“否”似乎循环回到第一个 If 语句。

谁能指出我正确的方向?我错过了什么?

inputText = str(input("Are you sure you want to delete file? (yes/no) "))
ctrl = True
while ctrl == True:
    if inputText != ('yes' or 'no'):
        inputText = input('Please type "yes" or "no" ')
    elif inputText == 'yes':
        ##delete
        print("pretend got deleted")
        ctrl = False
    elif inputText == 'no':
        ##shit does not get deleted
        print("pretend doesn't got deleted")
        ctrl = False

我预期的结果将是一个简单的

>> Are you sure you want to delete file? (yes/no) *random text that isn't 'yes' or 'no'*
>> Please type "yes" or "no"

这就是我在最初的 If 语句示例中得到的结果:

>> Are you sure you want to delete file? (yes/no) yes
>> pretend got deleted

这是我的预期:

>> Are you sure you want to delete file (yes/no) no
>> pretend doesn't got deleted

这是我实际得到的

>> Are you sure you want to delete file (yes/no) no
>> Please type "yes" or "no" 

即使我这样做:

>> Are you sure you want to delete file (yes/no) no
>> Please type "yes" or "no" yes
>> pretend got deleted

是我得到的结果

【问题讨论】:

  • 你可以使用if (inputText != 'yes') and (inputText != 'no'):
  • @Lyncius。看看我的回答,如果你不关注,请告诉我

标签: python-3.x if-statement


【解决方案1】:

问题在于您的第一个 if 语句。您的第一个 if,if inputText != ('yes' or 'no') 在看到您的输入不是 yes 时立即解析为 true。因此,仅当输入不是 yes 而不是解析为 if inputText != ('yes') and inputText != ('no')no 时才将 if 更改为 true

您的代码如下所示

inputText = str(input("Are you sure you want to delete file? (yes/no) "))
ctrl = True
while ctrl == True:
    if inputText != ('yes') and inputText != ('no'):
        inputText = input('Please type "yes" or "no" ')
    elif inputText == 'yes':
        ##delete
        print("pretend got deleted")
        ctrl = False
    elif inputText == 'no':
        ##shit does not get deleted
        print("pretend doesn't got deleted")
        ctrl = False

【讨论】:

  • 我试过了,结果与我的结果相反,“否”If 语句通过但“是”If 语句不再起作用,它直接回到最初的 if声明
  • @Lyncius 我的错。我构建了导致问题的 if wrong 。我已经更新了我的答案。现在就试一试
  • 就像一个魅力!好的酷。公平地说,如果我再次查看您的第一段,我会注意到同一行代码。我仍在试图理解为什么 if inputText != ('yes' or 'no') 不起作用。
  • 以您输入no 的情况为例,因此控件进入此if 块,并且由于此表达式是or 条件,如果语句的一部分为真,则认为它为真。因此,如果它不是 yes 并且当您输入 no 时,它会进入块并进行比较,它会认为它不是 yes 并打印出来
  • 如果您觉得它已经为您解决了问题,请不要忘记接受并投票赞成:)
【解决方案2】:

这里的问题在于你的逻辑,特别是这一行:

inputText != ('yes' or 'no')

('yes' or 'no') 解析为 'yes',因为这是第一个计算结果为 True 的元素(在布尔上下文中) - 换句话说,该行在功能上等同于块代码如下:

if bool('yes') is True:
    compare_value='yes'
else:
    compare_value='no'
if inputText != compare_value:
    inputText = input('Please type "yes" or "no" ')

你可能打算这样做:

inputText = str(input("Are you sure you want to delete file? (yes/no) "))
ctrl = True
while ctrl == True:
    if inputText not in ('yes', 'no',):
        inputText = input('Please type "yes" or "no" ')
    elif inputText == 'yes':
        ##delete
        print("pretend got deleted")
        ctrl = False
    elif inputText == 'no':
        ##shit does not get deleted
        print("pretend doesn't got deleted")
        ctrl = False

这里有几点说明:

  1. 现在您正在测试 inputText 是否不在包含 'yes' 和 'no' 的值集中( inputText not in ('yes','no',) )

    李>
  2. 注意该集合中的尾随逗号 ('yes','no',)(就在右括号之前) - 在 Python 中, () 用于操作顺序以及定义集合 -因此,如果您在括号中添加一个值 ('yes') => 这与 'yes' 相同。尾随的逗号 ('yes',) 将使 Python 意识到您打算定义一个集合 - 并认为它是这样的。现在,严格来说,如果有多个值,最后不需要逗号,即 ('yes','no') 可以 - 但这是养成习惯的“最佳实践”总是在后面加上逗号,这样您就不会意外地尝试在某个点定义单个元素集并最终得到一个值。

【讨论】:

  • 我理解 ("yes","no") 背后的概念。如果我是对的,这就像按照给定的顺序声明对这些的检查? (如我错了请纠正我)。但是后面的逗号?这有点……令人困惑。
  • @lyncius - 尝试这样做 - print(('yes' or no')) - 它总是会打印 yes,因为“或”表达式在 python 中转换为“返回第一个计算结果为在布尔上下文中为真”此外,一旦您查看答案,请务必支持/接受对提出相同/相似问题的 stackoverflow 用户最有帮助的答案。
  • @Lyncius - ("yes", "no",) 是一组值。表达式“inputText not in ("yes","no",)”表示“检查 inputText 是否不是(允许的)值集合中的值之一” - 如果它不在集合中,则评估为真;否则计算为 False。
猜你喜欢
  • 1970-01-01
  • 2019-04-19
  • 1970-01-01
  • 2013-07-26
  • 1970-01-01
  • 2015-02-24
  • 2012-10-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多