FAQ: Introduction to Functions - Multiple Returns

This community-built FAQ covers the “Multiple Returns” exercise from the lesson “Introduction to Functions”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Visualize Data with Python
Data Scientist
Analyze Data with Python
Computer Science
Analyze Financial Data with Python
Build Chatbots with Python
Build Python Web Apps with Flask
Data Analyst

Learn Python 3
CS101 Livestream Series

FAQs on the exercise Multiple Returns

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Learn more about how to use this guide.

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

weather_data = ['Sunny', 'Sunny', 'Cloudy', 'Raining', 'Snowing']

 

def threeday_weather_report(weather):

  first_day = " Tomorrow the weather will be " + weather[0]

  second_day = " The following day it will be " + weather[1]

  third_day = " Two days from now it will be " + weather[2]

  return first_day, second_day, third_day

monday, tuesday, wednesday = threeday_weather_report(weather_data)

 

print(monday)

print(tuesday)

print(wednesday)

The program works as intended, but my question is: "Where does the function get the weather data from? Shouldn’t we define it with first_day = " Tomorrow the weather will be " + weather_data[0] instead of
first_day = " Tomorrow the weather will be " + weather[0]
?

Later Edit: I understood where it gets it from now: when we call the function for the three values.

threeday_weather_report is the name of the function. It has one parameter called weather. This means that whatever value is passed to this function will be assigned to the variable called weather. This is very useful because it allows us abstraction and re-use of the function. We don’t care about the name of the value being passed in to the function. Whatever it may be called outside the function doesn’t matter to us. Within our function, we will simply assign that value to a variable named weather. That way we don’t have to change our function for different inputs.

In the snippet you posted weather_data is a list. When you make the function call

monday, tuesday, wednesday = threeday_weather_report(weather_data)

weather_data is being passed in as an argument. This list will be assigned to our function parameter weather and within our function we will use the name weather to work with this data.
Suppose our original list was named usefulStuff. We would change our function call to

monday, tuesday, wednesday = threeday_weather_report(usefulStuff)

but we won’t have to change a single thing in our function. We will simply assign the argument usefulStuff to our parameter weather.

3 Likes

Thanks a lot! I missed the function call when defining the three variables.

Question. I’m working on the problem that is using multiple returns to list travel locations in Italy by popularity (1, 2, 3)
it SHOULD return:

Rome
Venice
Florence

I am not getting syntax errors with this though. Can someone tell me what is wrong with this code?

def top_tourist_locations_italy():

first = “Rome”

second = “Venice”

third = “Florence”

return first, second, third = most_popular1, most_popular2, most_popular3

print(most_popular1)

print(most_popular2)

print(most_popular3)

Hi, your code should be like this:

def top_tourist_locations_italy():
first = “Rome”
second = “Venice”
third = “Florence”
return first, second, third

most_popular1, most_popular2, most_popular3 = top_tourist_locations_italy()

print(most_popular1)
print(most_popular2)
print(most_popular3)


I think you cannot assign the values implicitly in the return

In the point 3, it says that “In order to use our 3 Return values from top_tourist_locations_italy() we need to assign them to new variables names after we call our function.” That’s why it’s asking us to create the variables: most_popular1…3.
So the syntax should be:
return1, return2, return3 = multi_return()

And also remember that when a variable is outside of any function it can be accessed anywhere in the code.

3 Likes

im curious then,
why wouldnt:
“top_tourist_locations_italy = most_popular1, most_popular2, most_popular3”
work? isnt it creating the same circumstances? albeit in a different method?

The original code had an issue where assignment was used in a return statement which is invalid syntax as @micro7289957357 mentioned. I’m not 100% sure what your query is so apologies if I’m wrong but you appear to be assigning the variables to the name of the function which isn’t what you’re looking for either.

You just want the function to return three values which is typically a return like- return a, b, c. By passing three names on the left hand side of the assignment you’re unpacking a three item tuple into three separate values: val1, val2, val3 = func().

2 Likes

ook, i gotcha! yah that makes perfect sense! thanks alot tgrtim :slight_smile:

1 Like

I want to confirm that I actually understand the code that has been deemed to be correct for this exercise:

My code for the “Top Tourists Location in Italy” exercise is:

def top_tourist_locations_italy():

first = “Rome”
second = “Venice”
third = “Florence”
return first, second, third

Here is my understanding of the above section:

  • I defined the “top_tourist_locations_italy” function
  • I assigned a value to the variable “first, second, third”
  • By using the return function, I ‘stored’ those variables so that they are able to be used later

most_popular1, most_popular2, most_popular3 = top_tourist_locations_italy()

Here is my understanding of the above section:

  • I set new variables (most_popular1, most_popular2, most_popular3) and assigned them so that they match the variables that have been returned (which are first, second, and third)
  • Thus, I “re-identified” the variables “first, second, third” to now be respectively “most_popular1, most_popular2, most_popular3”

print(most_popular1)
print(most_popular2)
print(most_popular3)

  • In the code above, I printed each variable out

Let me know if I have made any mistakes or misinterpretations of the code :slight_smile:

Looks right to me. Though, I still don’t understand the point of this exercise. Why must we make new variables (most_popular1,2,3…) to begin with? Why not use the first, second, third variables?

We choose the running variables. Any time we can use an expression is a chance to omit a new variable. You raise a good point.

1 Like

Indeed, I understand that the course is aiming to make us practice multiple return statements,
but I guess we could achieve the same result with:

for location in top_tourist_locations_italy():
  print(location)

Or even, to some extent, this:

print(top_tourist_locations_italy())

@mtf , I know you like providing extra examples to help us progress.
Do you know of a short, yet more complex, piece of code that uses multiple returns
to better show their efficiency?

Why is [0], [1], [2] used behind our weather parameter?

weather is a list containing five values. We want the first three that correspond with monday, tuesday, wednesday at the caller (the variables that will receive the returns).

Sorry for the delay. Must have missed this question from two weeks ago.

More complex? Not sure. This concept is fairly straight forward since it is all written in the return statement and unpacked at the caller. If the number of variables does not match the number of returns, an error will be raised. That is, unless there is only one variable, in which case the values will be packed in a tuple.

>>> def foo(ref):
	return ref[0], ref[1], ref[2]

>>> a, b = foo([1, 2, 3, 4, 5, 6])
Traceback (most recent call last):
  File "<pyshell#143>", line 1, in <module>
    a, b = foo([1, 2, 3, 4, 5, 6])
ValueError: too many values to unpack (expected 2)
>>> a, b, c = foo([1, 2, 3, 4, 5, 6])
>>> a
1
>>> b
2
>>> c
3
>>> z = foo([1, 2, 3, 4, 5, 6])
>>> z
(1, 2, 3)
>>> a, b, c = z
>>> a
1
>>> b
2
>>> c
3
>>> 
2 Likes

No problem, I have been keeping busy as well!
This is crystal clear: thank you for the extra help, as usual :slight_smile:

1 Like

I am having some trouble with this exercise. I am pretty sure my code is right, and when I clicked run on the last activity the check box turned green, but I saw no output in the terminal. I am not sure what I have missed in my code:

def top_tourist_locations_italy(): first = "Rome" second = "Venice" third = "Florence" return first, second, third most_popular1, most_popular2, most_popular3 = top_tourist_locations_italy() print(most_popular1) print(most_popular2) print(most_popular3) top_tourist_locations_italy()

lines 6 thru 9 should be outside of the function. The last line (11) will not be needed since the call is made in line 6.

2 Likes

Thank you! I think I understand, line 6 is supplying the arguments most_popular 1, 2, and 3 to the function - it is not part of the function.

1 Like