PyCharm错误名称“chicks”可以是未定义的

2024-05-19 05:21:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习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")

Tags: 代码inyouinputifchickhavearray
1条回答
网友
1楼 · 发布于 2024-05-19 05:21:28

尝试在开头添加新变量“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的乐趣

相关问题 更多 >

    热门问题