Python测试字符串是否为一组特定值之一

Python测试字符串是否为一组特定值之一,第1张

Python测试字符串是否为一组特定值之一

你忘了写

s == "no"
你的第一本
elif:

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":  #<---thiselif s == "No" or True or True:  #<---is the same as this

由于这是公认的答案,因此我将详细介绍标准做法:不考虑大小写而比较字符串的约定(equalsIgnoreCase)的用法

.lower()
如下:

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


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5639693.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存