PyCharm 错误 - 名称 'chicks' 可以未定义

PyCharm Error - Name 'chicks' can be undefined

我正在学习 Python 并遵循了 Tech with Tim 的教程系列,然后我制作了自己的 python 代码,询问您喜欢什么动物,如果有,是什么是它的年龄,它的名字是什么,我在 PyCharm 上遇到了这个错误。名称 'chicks' 可以未定义。

我只有 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")

尝试在开头添加一个新变量"chicks":

# variables
chicks = ''
animal = input("What animal do you like? ")
animal_array = ["Chicks", "chicks"]
chicks_array = ["Yes", "yes"]
other_animals = [""]
yes_answer = ["Yes", "yes"]

这样,即使你不说你有小鸡,变量仍然被定义。

祝您学习愉快python!