FAQ: Introduction to Functions - Variable Access

This community-built FAQ covers the “Variable Access” 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 Variable Access

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!

I thought this exercise was a little basic, so I had fun modifying it.
Here’s a alternate way to make use of lists and the len() function:

# This function will print a hardcoded count of how many locations we have.
favorite_locations = ["Paris", "Norway", "Iceland"]

def print_count_locations():
  length = len(favorite_locations)
  print("There are " + str(length) + " locations.")
    
# This function will print the favorite locations
def show_favorite_locations():
  print("Your favorite locations are: " + str(favorite_locations) + ".")

print_count_locations()
show_favorite_locations()

I am not following why I didn’t have to define the locations?
From the previous lessons I was under the impression that all:
def anything_before_the_parameter(p1, p2, p3) - needed to be defined.

So my first thought was to add parameters.

So now looking at the code solutions it was defining variable before the parameter which didn’t need to be defined at all?

Was it predefined in the code for the exercise? All previous exercises in this lesson has parameters and arguments being utilized.

A function may be defined with no parameters, but the parameter list is still required.

def func():
    pass

Just so I am clear. I can call anything outside the function as long as the variable is identified?

Outer scope is accessible for read-only, but yes, we can call anything, whether function or variable. If it doesn’t exist in that scope then a ReferenceError will be raised.

def main():
    def foo():
        x = a * b
        return x
    def bar():
        y = a + b
        return y
    a, b = 6, 7
    return foo(), bar()

Above both foo and bar can see a and b. They can’t see each other’s local variables, x and y, and neither can they be seen from the scope of a and b.

that would output the same thing right?

>>> a, b = main()
>>> c = "".join(sorted(list(str(a)) + list(str(b))))
>>> c
'1234'
>>> d = int(c)
>>> d
1234
>>> 

I typed the code exactly the same way:

favorite_locations = [“Paris”, “Norway”, “Iceland”]

def print_count_locations():
length = len(favorite_locations)
print("There are " + str(length) + “locations.”)

def show_favorite_locations():
print("Your favorite locations are: " + str(favorite_locations) + “.”)

print_count_locations()
show_favorite_locations()

I get this print out:
There are 3 locations.
Your favorite locations are: (‘Paris’, ‘Norway’, ‘Iceland’).

and this error code"
The output should be Your favorite locations are: Paris, Norway, Iceland

What am I doing wrong?

Python will represent the entire sequence as a tuple in a string. To merge the list of strings with one string, concatenate with a joined list.

>>> favorite_locations = ["Paris", "Norway", "Iceland"]
>>> print (f"Your favorite locations are: {', '.join(favorite_locations)}")
Your favorite locations are: Paris, Norway, Iceland
>>> 

If str.join() hasn’t been covered yet, then you’ll need to use a loop to construct the return string.

I got this instead:

There are 3 locations.
Your favorite locations are: [‘Paris’, ‘Norway’, 'Iceland"].

I get this instead:

File “travel.py”, line 10
print(f"Your favorite locations are: {’, '.join(favorite_locations)"}
^
SyntaxError: invalid syntax

The closing parenthesis is missing from the print statement.

This function will print a hardcoded count of how many locations we have.

favorite_locations = [“Paris”, “Norway”, “Iceland”]

def print_count_locations():
length = len(favorite_locations)
print(“There are " + str(length) + " locations.”)

This function will print the favorite locations

def show_favorite_locations():
print("Your favorite locations are: " + str(favorite_locations))

print_count_locations()
show_favorite_locations()

outcome:

There are 3 locations.
Your favorite locations are: [‘Paris’, ‘Norway’, ‘Iceland’]

I just need to figure out how to get rid of the parenthesis and print: Your favorite locations are: Paris, Norway, Iceland

If we cannot use .join then we need to leverage Python’s print() ability to hold the draw pencil on the same line.

>>> favorite_locations = ["Paris", "Norway", "Iceland"]
>>> def show_favorite_locations():
	print ("Your favorite locations are", end=': ')
	for x in favorite_locations[:-1]:
		print (x, end=', ')
	print (favorite_locations[-1])

	
>>> show_favorite_locations()
Your favorite locations are: Paris, Norway, Iceland
>>>