Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Functions, Packing, and Unpacking Introduction to Functions Let's Talk About Scope

I'm confused... When I run python, there is no output in the terminal.

Not 5, not 10, just nothing. That makes it hard for me to understand the difference between global and local scope, and why the print function doesn't work even when we call the function.

Josh Keenan
Josh Keenan
19,652 Points

Can you post some code please.

3 Answers

Hi Nicolas - this is because the set_num() function is ONLY setting a local variable of num inside the function to 5, and not printing it to the console. If you wanted to test how local vs global scope works, you could do something like this:

num = 10


def set_num():
    num = 5
    letter = 'a'
    print(letter)
    print(num)


set_num()
print(num)

^ In the code above, the first line creates a variable called num that is equal to an int of 10. Next, we define a function called set_num() that creates a local variable called num and sets its value to an int of 5. Next, we set create a local variable called letter and set its value to a string, 'a'. The final two lines of code in the function print the values stored for the local variables to the console. To see how this works, we can call the function set_num() and then print the value stored in the global value of num.

You should see an output of: 'a' 5 10

Leah Kelly
Leah Kelly
4,141 Points

have you saved the program yet?

There is no output in the terminal the FIRST time the instructor asks you to pause the video and try the code because the instructor forgot to add a print() command.