我一直在为这个学校做这个任务,我只是无法弄清楚为什么我不能让这个程序正常工作。我正试图让程序允许用户输入三个动物。它只允许我输入一个。我知道这与我在make_list函数中放置return语句有关,但无法弄清楚如何解决它。for循环中的return语句
这里是我的代码:
import pet_class
def main():
#get list of pet objects
pets = make_list()
#Display the data in a list.
print 'Here is the data you entered:'
display_list(pets)
#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:')
print
#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
#The display_list function accepts a list containing pet objects
#as an argument and displays the data stored in each object.
def display_list(pet_list):
for item in pet_list:
print item.get_name()
print item.get_animal_type()
print item.get_age()
print
#call main function
main()
稍微详细一点会有所帮助:make_list()中的for循环会继续执行一次,然后返回。在python缩进很重要。 – 2011-05-03 01:32:47