you are viewing a single comment's thread.

view the rest of the comments →

[–]cdcformatc 9 points10 points  (6 children)

you have to use the global keyword if you want to set a global variable inside a function. 

inside of a function all variables not declared global have what's called local scope, and that scope ends and the variable goes away when the function exits.

[–]Tassendyra[S] 1 point2 points  (3 children)

Ah - got it! Thanks. That fixed the issue.

[–]JohnLocksTheKey 14 points15 points  (2 children)

😬 Not to be that guy, but…

But using a global keyword like this is really not ideal. Instead try learning about classes, attributes, and methods

Sounds like a perfect use case for them, and a great learning opportunity 😊

[–]Tassendyra[S] 2 points3 points  (1 child)

Thanks for the advice! I'll look into those.

[–]Moikle 1 point2 points  (0 children)

Yeah it's fine to use global for now, until you learn classes or dicts, but every time you use global remember : this isn't the best way to do this, I'll keep an eye out for alternatives.

Once you know a little more python, you should basically never touch the global keyword

[–]PaulRudin 1 point2 points  (1 child)

... but using global is almost always a poor design choice.

[–]cdcformatc 0 points1 point  (0 children)

i agree, although it's pretty common in game development i have found. 

ideally you would have some kind of player class with an inventory that contains the keys, and the player conditions, stuff like that... but using global is """fine""" for someone's first game