[英]Why does it say response is not defined even though it is
它一直告诉我响应没有定义。
while response not in Yes or No:
response = input("Would you like to shut down the program?\nyes/no\n")
if response == "Yes":
print("Goodbye.\n")
elif response == "No":
print("Ok.")
quit()
else:
print("I didn't understand that.\n")
假设在您提供的代码上方未引用它,则第一次通过循环时,未定义response
。 只有在第一次调用input()
之后。 您需要在循环之前进行初始化。 类似response = None
就足够了。
由于 Python 区分大小写,因此将响应转换为大写。
response.upper() == "是":
response.upper() == “否”:
response = input("Would you like to shut down the program?\nyes/no\n")
if response.upper() == "YES":
print("Goodbye.\n")
elif response.upper() == "NO":
print("Ok.")
#quit()
else:
print("I didn't understand that.\n")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.