简体   繁体   English

为什么它说响应没有定义,即使它是

[英]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.

相关问题 为什么即使安装了 FBX 模块也找不到? - Why does it say FBX module not found even though it is installed? 为什么说“水平”没有定义 - why does it say “level” is not defined 为什么它说“条目”没有定义? - Why does it say that 'entry' is not defined? 为什么它显示变量“img”没有定义,即使它是全局的? - Why does it show that the variable "img" is not defined even though it is global? 为什么我的变量返回无,即使被定义 - Why does my variable return None, Even though being defined 为什么它说它输入了错误的代码,即使它与密钥相同 - Why does it say its the wrong code entered even though its the same as the key 为什么我的 pygame 说它没有初始化,即使我初始化了它? - Why does my pygame say it's not initialized even though I initialized it? 为什么当我问是否要再做一次时,即使我说“不”,它为什么也掷骰子? - Why does it roll the dice even though I say “no” when it asks if I want to do it again? 为什么说“名称'灰色'无法定义”? - Why does it say “Name 'gray' can be not defined”? 当父类方法在python中定义了子对象时,为什么后来定义了它,为什么还能工作呢? - Why does it work when a parent class method access a child object in python even though it is defined later?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM