Return statement for for loop

I am working on this assignment for school, and I just cannot understand why I cannot get this program to work correctly. I am trying to make the program allow the user to enter three animals. It only allows me to enter one. I know that this is due to my placement of the return statement in the make_list function, but cannot figure out how to fix it.

Here is my code:

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()
+5
source share
6 answers

I only answer this again because I notice that your subject is already reporting a problem and no one has given a theoretical explanation here. The theoretical word is too big in this case, but anything.

, return for. For-loop . , , . , , :

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

, , , ( , ). , , , return AFTER for-loop, , ,

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

    return ret

( , )

+22

pet_list for, .

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
+7

. , for. .

+3

.

+2

, for , return if

I had a similar problem right now with my code:

Returns the number of even ints in the given array. Note: the% "mod" operator calculates the remainder, for example. 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 want to visualize the execution and paste my code http://www.pythontutor.com/visualize.html#mode=edit

As soon as I canceled this 8 spaces (the same depth as the for statement), it was executed several times and gave the correct result.

+1
source

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

0
source

All Articles