I've been teaching 8th-9th graders basic computer programming for two weeks, and yesterday I tried to show them how they could make real simple text-adventure games in Python.
Scenes are functions, (e.g dragons_cave()
) which consist of some print statements and then a call to input()
, asking the player where they want to go next, which then gets passed to globals()
to find the appropriate function and then called. I know it's not ideal (at what point would the huge chain of functions start becoming a problem?) but, of what crossed my mind, it seems to be the simplest for them while involving only a little handwaving.
My problem is with global state — ex. the player gets a key in one scene and only then can they unlock the gate in another scene. When I have global immutables like strings or booleans, Python wants me to use the global
keyword at the beginning of the function.
global hasKey
hasKey = True
I'm pretty okay with that, but I have a vague sense (picked up from Stackoverflow among other places on the Internet) that global
is frowned upon and always has a superior counterpart. I could have a global dictionary or wrap everything in a class, but I'm not sure if I could defend those options clearly to my kids (who are still thinking through the implications of variables).
Whatever I use, I want to be able to explain straightforwardly to my kids why we do it this way and why doing it this way is necessary. global
seems to have both these properties, but is it bad?