【发布时间】: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”,它也会打印“关机中止!”虽然它应该打印“对不起,我不明白你”。
我做错了什么?
【问题讨论】:
-
谢谢你们,我会在几分钟内接受答复。你帮了很多忙,我想我再也不会忘记“==”了,哈哈