编写一个函数shut_down,它接受一个参数(你可以使用你喜欢的任何东西;在这种情况下,我们使用s作为字符串).当shut_down函数获得"是","是"或"是"作为参数时,应该返回"关闭...",并且"关闭已中止!" 当它变为"否","否"或"否"时.
如果它得到的不是那些输入,该函数应该返回"抱歉,我不明白你."
这是我的代码到目前为止......它的错误并说"不"不会返回"关机中止!"
def shut_down(s):
if s == "Yes" or "yes" or "YES":
return "Shutting down..."
elif s == "No" or "no" or "NO":
return "Shutdown aborted!"
else:
return "Sorry, I didn't understand you."
Run Code Online (Sandbox Code Playgroud)
grc*_*grc 11
这个:
s == "Yes" or "yes" or "YES"
Run Code Online (Sandbox Code Playgroud)
相当于:
(s == "Yes") or ("yes") or ("YES")
Run Code Online (Sandbox Code Playgroud)
这将始终返回True,因为非空字符串是True.
相反,您希望s单独与每个字符串进行比较,如下所示:
(s == "Yes") or (s == "yes") or (s == "YES") # brackets just for clarification
Run Code Online (Sandbox Code Playgroud)
它应该像这样结束:
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":
return "Shutdown aborted!"
else:
return "Sorry, I didn't understand you."
Run Code Online (Sandbox Code Playgroud)
您可以通过以下几种方式实现:
if s == 'Yes' or s == 'yes' or s == 'YES':
return "Shutting down..."
Run Code Online (Sandbox Code Playgroud)
要么:
if s in ['Yes', 'yes', 'YES']:
return "Shutting down..."
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
40952 次 |
| 最近记录: |