[英]Why does it say response is not defined even though it is
It keeps telling me response is not defined.它一直告诉我响应没有定义。
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")
Assuming it's not referenced above the code you provide, the first time through the loop, response
is not defined.假设在您提供的代码上方未引用它,则第一次通过循环时,未定义
response
。 Only after the first call to input()
.只有在第一次调用
input()
之后。 You need to initialize before the loop.您需要在循环之前进行初始化。 Something like
response = None
should suffice.类似
response = None
就足够了。
Since Python is case sensative, converted response to upper case.由于 Python 区分大小写,因此将响应转换为大写。
response.upper() == "YES": response.upper() == "是":
response.upper() == "No": 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.