12

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()
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Brad White
  • 129
  • 1
  • 1
  • 4

6 Answers6

31

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())

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Lacrymology
  • 2,269
  • 2
  • 20
  • 28
9

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
Acorn
  • 49,061
  • 27
  • 133
  • 172
2

You have the return statement at the incorrect level of indentation. It should be at the same depth as the for statement. Having the return within the loop causes it to break out of the loop.

Dan
  • 29
  • 1
1

Remove one indent before the return.

Daniel
  • 30,896
  • 18
  • 85
  • 139
  • 2
    A little more detail would help: The for loop in make_list() is going thorugh once and then returning. In python indenting is important. – James Khoury May 03 '11 at 01:32
0

You notice the for loop runs only once because the return statement is inside the if statement, within the loop

I've had a similar problem right now with my code:

Return the number of even ints in the given array. Note: the % "mod" operator computes the remainder, e.g. 5 % 2 is 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]) 

if you go to visualize execution and paste in my code http://www.pythontutor.com/visualize.html#mode=edit

Once I unindented it 8 spaces (same depth as the for statement), it ran multiple times and gave the correct output.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
noob81
  • 65
  • 1
  • 11
-1

Your spacing is off. return pet_list is in the scope of the for loop.

pllee
  • 3,909
  • 2
  • 30
  • 33