'return statement in for 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()
Solution 1:[1]
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())
Solution 2:[2]
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
Solution 3:[3]
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.
Solution 4:[4]
Remove one indent before the return.
Solution 5:[5]
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.
Solution 6:[6]
Your spacing is off. return pet_list is in the scope of the for loop.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | wjandrea |
| Solution 2 | Acorn |
| Solution 3 | Dan |
| Solution 4 | Daniel |
| Solution 5 | OneCricketeer |
| Solution 6 | pllee |
