我正在自学Python,有以下问题:
这是我的“清单”:
animal_name
num_animals
all_animals
已编辑 我编写了以下代码,但它不起作用。我需要改变什么,这样动物的每一次输入都会被计数
animal_name = input("Put in 4 animal names in total per time or \"exit\" for break")
num_animals = 0
tot_animals = 4
all_names = ""
while num_animals <= tot_animals:
print(num_animals, "now")
num_animals += 1
all_animals += animal_name + "," # connect the name to all names.
if animal_name.lower() == ("exit"):
break
if len(all_animals) == 0: # len is 0, then it's empty
print("no animals")
print("names of all animals: ", all_animals)
来自@Zhd Zilin的改进代码
更新程序,请参阅while语句行的注释
这应该是可行的
另外,要使您的代码干净,改进安排以便于调试
相关问题 更多 >
编程相关推荐