Using a return inside of a loop will break it and exit the function even if the iteration is still not finished.
For example:
def num():
# Here there will be only one iteration
# For number == 1 => 1 % 2 = 1
# So, break the loop and return the number
for number in range(1, 10):
if number % 2:
return number
>>> num()
1
In some cases we need to break the loop if some conditions are met. However, in your current code, breaking the loop before finishing it is unintentional.
Instead of that, you can use a different approach:
Yielding your data
def show_todo():
# Create a generator
for key, value in cal.items():
yield value[0], key
You can call it like:
a = list(show_todo()) # or tuple(show_todo())
or you can iterate through it:
for v, k in show_todo(): ...
Putting your data into a list or other container
Append your data to a list, then return it after the end of your loop:
def show_todo():
my_list = []
for key, value in cal.items():
my_list.append((value[0], key))
return my_list
Or use a list comprehension:
def show_todo():
return [(value[0], key) for key, value in cal.items()]
Answer from Chiheb Nexus on Stack OverflowUsing a return inside of a loop will break it and exit the function even if the iteration is still not finished.
For example:
def num():
# Here there will be only one iteration
# For number == 1 => 1 % 2 = 1
# So, break the loop and return the number
for number in range(1, 10):
if number % 2:
return number
>>> num()
1
In some cases we need to break the loop if some conditions are met. However, in your current code, breaking the loop before finishing it is unintentional.
Instead of that, you can use a different approach:
Yielding your data
def show_todo():
# Create a generator
for key, value in cal.items():
yield value[0], key
You can call it like:
a = list(show_todo()) # or tuple(show_todo())
or you can iterate through it:
for v, k in show_todo(): ...
Putting your data into a list or other container
Append your data to a list, then return it after the end of your loop:
def show_todo():
my_list = []
for key, value in cal.items():
my_list.append((value[0], key))
return my_list
Or use a list comprehension:
def show_todo():
return [(value[0], key) for key, value in cal.items()]
Use a generator syntax (excellent explanation on SO here):
def show_todo():
for key, value in cal.items():
yield value[0], key
for value, key in show_todo():
print(value, key)
Videos
def function():
while True:
result = do_a_thing()
if result == "all done":
returnOr
def function():
while True:
result = do_a_thing()
if result == "all done":
break?
They both have the same outcome. Would one be faster than the other? Which would the reader prefer?
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())
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