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

标签 python pycharm jetbrains-ide

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

相关文章:

intellij-idea - 如何在Jetbrains Rider 2018.1中强制执行LF行结尾

html - PhpStorm - 自动关闭和自动完成 HTML 标签,而无需在开头输入 "<"

maven - Idea 的 cmd 和 git bash 看不到 mvn 命令

python - 如何在 for 循环中将 DataFrames 添加到 Excel 工作簿?

java - PyLucene 尝试创建 FSDirectory 时出现 InvalidArgsError

python - 导入错误 : No module named 'tensorflow.contrib.lite.python.tflite_convert'

python - 如何设置 PyCharm 不打开所有最后的项目?

pycharm - pycharm 中的 Jupyter 笔记本在 Pycharm 中不支持本地历史记录?

python - 如何在python中获取列表的最小和最大元素

python - 在 PyCharm 与交互式 shell 中运行模块