Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return statement in for loops

Tags:

python

loops

I have been working on this assignment for school and I just can't figure out what why I cant get this program to work properly. I am trying to get the program to allow the user to enter three animals. It is only allowing me to enter one. I know it has to do with my placement of the return statement in the make_list function but can't figure out how to fix it.

Here is my code:

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()
like image 227
Brad White Avatar asked May 03 '11 01:05

Brad White


People also ask

What is the return type of for loop?

Java for-each Loop It returns element one by one in the defined variable. Syntax: for(data_type variable : array_name){ //code to be executed.

Can I use return in for loop in C?

The answer is yes, it will return.

Can I use return in a for loop Python?

Of course. “return” means to stop the execution of the function/method right here! So, it can be anywhere in the method, whether at the end of the method/function or in a loop, etc.

Can we return inside for loop in Java?

The return statement inside a loop will cause the loop to break and further statements will be ignored by the compiler.


2 Answers

Your problem is, precisely, that you're putting the return statement inside the for-loop. The for-loop runs each statement in it for however so many times.. if one of your statements is a return, then the function will return when it hits it. This makes sense in, for example, the following case:

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

Here, the function iterates until it finds where the needle is in the haystack, and then returns that index (though there's a builtin function to do this, anyways, list.index()).

If you want the function to run for however many times you tell it to, you have to put the return AFTER the for-loop, not inside it. That way, the function will return after the control gets off the loop

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

(though again, there's a builtin function to do this as well, sum())

like image 103
Lacrymology Avatar answered Sep 22 '22 16:09

Lacrymology


You just need to return the pet_list outside of the for loop, so it will happen after the loop has finished running.

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
like image 44
Acorn Avatar answered Sep 19 '22 16:09

Acorn