我正在学习 Python 并遵循了 Tech with Tim 的系列教程然后我编写了自己的 python 代码,询问你喜欢什么动物,如果你有它,它的年龄,它的名字是什么,我在 PyCharm 上得到了这个错误。名称“小鸡”可以未定义。
我只有 7 岁,所以如果我错过了什么,请放轻松,下面是我的代码:
# variables
animal = input("What animal do you like? ")
animal_array = ["Chicks", "chicks"]
chicks_array = ["Yes", "yes"]
other_animals = [""]
yes_answer = ["Yes", "yes"]
# if statements
if animal in animal_array:
print("Me too!")
chicks = input("Do you have a chick? ")
else:
print("Ok")
animal_q = input("Do you have a/an/some " + animal + "? ")
if chicks in chicks_array:
chick_name = input("What is your chick's name? ")
chicks_age = input("What is your chick's age? ")
else:
print("Ended")
最佳答案
尝试在开头添加一个新变量“小鸡”:
# variables
chicks = ''
animal = input("What animal do you like? ")
animal_array = ["Chicks", "chicks"]
chicks_array = ["Yes", "yes"]
other_animals = [""]
yes_answer = ["Yes", "yes"]
这样,即使你没有说你有一个小鸡,变量仍然被定义。
玩得开心学习python!
关于python - PyCharm 错误 - 名称 'chicks' 可以未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62309349/