【问题标题】:Python testing whether a string is one of a certain set of values [duplicate]Python测试字符串是否是一组特定值之一[重复]
【发布时间】:2021-07-07 06:21:04
【问题描述】:

我正在 codecademy 上学习 python,我目前的任务是:

编写一个函数,shut_down,它接受一个参数(你可以使用 你喜欢的都可以;在这种情况下,我们将 s 用于字符串)。关机 函数应该返回 "Shutting down..." 当它得到 "Yes", "yes", 或 "YES" 作为参数,"Shutdown aborted!" 当它变为 "No" 时, “否”,或“否”

如果它得到的不是这些输入,函数应该 return “对不起,我没听懂你的意思。”

对我来说似乎很容易,但不知何故我还是做不到。

我为测试该功能而编写的代码:

def shut_down(s):
    if s == "Yes" or s == "yes" or s == "YES":
        return "Shutting down..."
    elif s == "No" or "no" or "NO":
        return "Shutdown aborted!"
    else:
        return "Sorry, I didn't understand you."

i = input("Do you want to shutdown?")
print(i) #was to test the input
print(shut_down(i)) #never returns "Sorry, I didn't understand you"

它适用于“否”和“是”,但不知何故,如果我在任何“是”之前放置一个空格,或者即使我只是输入“a”,它也会打印“关机中止!”虽然它应该打印“对不起,我不明白你”。

我做错了什么?

【问题讨论】:

  • 谢谢你们,我会在几分钟内接受答复。你帮了很多忙,我想我再也不会忘记“==”了,哈哈

标签: python string equality


【解决方案1】:

你忘了在你的第一个elif:中写s == "no"

def shut_down(s):
    if s == "Yes" or s == "yes" or s == "YES":
        return "Shutting down..."
    elif s == "No" or "no" or "NO":             # you forgot the s== in this line
        return "Shutdown aborted!" 
    else:
        return "Sorry, I didn't understand you."

这样做:

def shut_down(s):
    if s == "Yes" or s == "yes" or s == "YES":
        return "Shutting down..."
    elif s == "No" or s == "no" or s == "NO":       # fixed it 
        return "Shutdown aborted!"
    else:
        return "Sorry, I didn't understand you."

这是因为:

elif s == "No" or "no" or "NO":  #<---this
elif s == "No" or True or True:  #<---is the same as this

由于这是公认的答案,我将详细说明包括标准做法:比较字符串而不考虑大小写(equalsIgnoreCase)的约定是像这样使用.lower()

elif s.lower() == "no":

【讨论】:

  • 谢谢,辛苦了。我不知道我必须在每个之前或在 python 中输入(在这个例子中)'s =='。
  • @Davlog 但是你在Yes 行中这样做了?!
  • @Davlog : 如果一个答案解决了你的问题,你应该接受它。
  • @Stephan 另一个选项可能是if s in ('Yes', 'yes', 'YES') resp。 if s in ('No', 'no', 'NO').
  • @Stephan 是的,我没有忘记这一点
【解决方案2】:

您可以使用lower 函数以小写形式返回s 的副本并与之比较,而不是检查不同的大小写组合。

def shut_down(s):
    if s.lower() == "yes":
        return "Shutting down..."
    elif s.lower() == "no":       
        return "Shutdown aborted!"
    else:
        return "Sorry, I didn't understand you."

这更干净,更容易调试。或者,您也可以使用upper 并与"YES""NO" 进行比较。


如果由于 nO 这样的匹配案例而这没有帮助,那么我会使用 in 声明:

def shut_down(s):
    if s in ("yes","Yes","YES"):
        return "Shutting down..."
    elif s in ("no","No","NO"):       
        return "Shutdown aborted!"
    else:
        return "Sorry, I didn't understand you."

【讨论】:

  • 我也会这样做,但任务非常具体。
  • @MatLecu 阅读 here 了解为什么会这样。
  • @MatLecu 你可能有一个观点,所以使用in添加了一个替代方案。
  • 对不起,我的措辞不好:我知道它为什么有效,但似乎作业说它不应该与 nO 一起使用。编辑仍然比一堆更好。
【解决方案3】:

Python 将非空字符串评估为True,因此您的elif 条件始终评估为True

>>> bool('No')
True

>>> bool('NO')
True

使用True 值执行布尔值or 将始终返回True,因此它永远不会达到else 条件并卡在elif 上。

你需要测试条件使用。

elif choice == 'no' or choice == 'NO' or choice == 'No':

编辑 - 正如 glglgl 在评论中指出的那样,== 的绑定比 or 更难,所以你的条件被评估为 (s == 'No') or 'no' or 'NO' 而不是 s == ('No' or 'no' or 'NO'),在这种情况下你会即使对于'NO' 的用户输入,也已到达else 部分。

【讨论】:

  • 也许值得注意的是,由于operator precedence rules== 绑定更加困难,因此它评估为(s == "No") or ("no") or ("NO") 而不是s == ("No" or "no" or "NO")。后一个可以归结为s == "No"
  • @glglgl :在答案中添加。谢谢。 :)
猜你喜欢
  • 1970-01-01
  • 2015-11-12
  • 2019-08-15
  • 1970-01-01
  • 2015-09-16
  • 1970-01-01
  • 1970-01-01
  • 2014-07-23
相关资源
最近更新 更多