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

How is 'character_dict' now already a defined variable if it's pointing to a whole dictionary??

1 Answer

Steven Parker
Steven Parker
243,318 Points

Since you create the variable "character_dict" inside the function, it can only be accessed by that function (it is limited to the function "scope"). If you want to make it as a global, you can put this line before line 6 (where you assign it):

    global character_dict

Also, you may want to wrap your call to "character_dict.items()" inside a "print" on line 51.

So it's kinda like how some other languages have variables that are private (not accessible to all), but others that are public (accessible to all)?

Steven Parker
Steven Parker
243,318 Points

Only slightly. The concepts of "public" and "private" access modifiers is a bit different from scope restrictions. Those languages that have them also have scope rules, too.

But I suppose you could say that in a sense, any variable created inside a function is "private" to that function unless you specifically declare that it should be a global.

I hope that cleared it up. And happy coding!

Thanks for all your help! You're very prominent in the community! Lol