【发布时间】:2011-08-17 09:00:54
【问题描述】:
我一直在为学校完成这项任务,但我无法弄清楚为什么我无法让该程序正常运行。我正在尝试让程序允许用户输入三种动物。它只允许我输入一个。我知道这与我在 make_list 函数中放置 return 语句有关,但不知道如何修复它。
这是我的代码:
import pet_class
#The make_list function gets data from the user for three pets. The function
# returns a list of pet objects containing the data.
def make_list():
#create empty list.
pet_list = []
#Add three pet objects to the list.
print 'Enter data for three pets.'
for count in range (1, 4):
#get the pet data.
print 'Pet number ' + str(count) + ':'
name = raw_input('Enter the pet name:')
animal = raw_input('Enter the pet animal type:')
age = raw_input('Enter the pet age:')
#create a new pet object in memory and assign it
#to the pet variable
pet = pet_class.PetName(name,animal,age)
#Add the object to the list.
pet_list.append(pet)
#Return the list
return pet_list
pets = make_list()
【问题讨论】:
-
这能回答你的问题吗? How to use a return statement in a for loop?