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.

2 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.