2011-05-03 130 views
5

我一直在为这个学校做这个任务,我只是无法弄清楚为什么我不能让这个程序正常工作。我正试图让程序允许用户输入三个动物。它只允许我输入一个。我知道这与我在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() 

回答

2

在返回之前删除一个缩进。

+2

稍微详细一点会有所帮助:make_list()中的for循环会继续执行一次,然后返回。在python缩进很重要。 – 2011-05-03 01:32:47

7

您只需要将for循环之外的pet_list返回,所以它会在循环结束运行后发生。

def make_list(): 
    pet_list = [] 

    print 'Enter data for three pets.' 
    for count in range (1, 4): 
     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 

     pet = pet_class.PetName(name,animal,age) 
     pet_list.append(pet) 

    return pet_list 
+0

非常感谢。令人惊讶的是如此简单的事情可以被忽略 – 2011-05-03 01:36:47

2

您在不正确的缩进级别返回语句。它应该与for语句处于同一深度。在循环中返回会导致它跳出循环。

0

您的间隔关闭。返回pet_list在for循环的范围内。

22

我只是再次回答这个问题,因为我注意到你的主题已经说明了问题,没有人给出理论上的解释,在这里,理论在这种情况下太大了,但是无论如何。

您的问题恰恰是您将return语句放在for循环中。 for循环会在其中运行每个语句,但是如此多次。如果其中一个语句是返回值,那么函数将在返回时返回。这是有道理的,例如,下面的情况:

def get_index(needle, haystack): 
    for x in range(len(haystack)): 
     if haystack[x] == needle: 
      return x 

这里,函数迭代,直到它找到其中的针在草垛,然后返回指数(有一个内置函数来做到这一点,反正)。如果你想让函数运行多次,你告诉它,你必须在for循环后面放置返回,而不是在它内部,这样,函数将在控制器离开循环后返回。

def add(numbers): 
    ret = 0 
    for x in numbers: 
     ret = ret + x 

    return ret 
def add(numbers): 
    ret = 0 
    for x in numbers: 
     ret = ret + x 

    return ret 

(再次,有一个内置函数来做到这一点为好)

1

您会注意到,因为return语句是if语句内的for循环只运行一次,循环

我已经内与我的代码现在类似的问题:

返回给定数组中的偶数个数。注:%“mod”运算符计算余数,例如5%2 1

count_evens([2, 1, 2, 3, 4]) → 3 
count_evens([2, 2, 0]) → 3 
count_evens([1, 3, 5]) → 0 

def count_evens(nums): 
    summary = 0 
    for i in nums: 

     if i % 2 == 0: 
      summary += 1 
      return summary  



count_evens([2, 1, 2, 3, 4]) 

如果你去可视化执行和在我的代码http://www.pythontutor.com/visualize.html#mode=edit

粘贴有一次,我缩进,这8位(相同深度for语句),它跑了多次,给正确的输出。