首页 > 解决方案 > 为什么它说响应没有定义,即使它是

问题描述

它一直告诉我响应没有定义。

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")

标签: python

解决方案


假设在您提供的代码上方没有引用它,则第一次通过循环时,response未定义。只有在第一次调用到input(). 您需要在循环之前进行初始化。类似的东西response = None就足够了。


推荐阅读