我正在学习Python,并跟随一个系列教程,由 技术与蒂姆 然后我自己做了一个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愉快!