12

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?

Eli Rose
  • 6,788
  • 8
  • 35
  • 55
  • there are plenty of good reasons to avoid global, there are also plenty of perfectly fine reasons to use globals. just my 2c ... for beginners it is an easier concept to grasp but can lead to bad habits in the future. – Joran Beasley Jul 20 '13 at 18:54
  • 2
    I'd vote you just be honest and upfront about it. "When you become more accomplished programmers, you'll learn that there are better methods than to use globals. For now, it helps us to keep things simple while we're learning the basics. If you'd like to learn how to do this without globals, ask me after class." Few people I know learned to walk wearing track shoes. – JS. Aug 26 '13 at 16:39

2 Answers2

10

I would encourage them to start learning OO

class Location:
     name="a place"
     description = "A dark place.  there are exits to the North and East"
     exits = "North","East"
     def __str__(self):
         return "%s\n%s"%(self.name,self.description)


class Player:
     current_location = "Home"
     inventory = ["Blue Key","Magic Thumbtacks"]
     health = 100
     name = "Unknown"
     def __init__(self,name):
         self.name = name

player = Player("Player 1")
loc = Location()
print loc
x = input("Input:")

To be honest a game is a difficult concept (even a text adventure). But I would start them directly on OO concepts, they will benefit more from that in the long term.

Granted this example is very small and leaves a lot of implementation details out.

An unrelated but better OO example would be:

class Animal:
     voice = '...'
     def speak(self):
         return "A %s Says '%s'"%(self.__class__.__name__, self.voice)

class Dog(Animal):
     voice = "Bark, Bark"

class Duck(Animal):
     voice = "Quack, Quack"

print Dog().speak()
print Duck().speak()
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
Joran Beasley
  • 110,522
  • 12
  • 160
  • 179
  • 1
    While encouraging OO design is a good idea, I'm not sure that classes with lots of class variables are good examples. That's basically just globals again, just bundled up in a class namespace (which is better than nothing, but still not really great). I suppose you can override the class variables in subclasses, but that's not a very common style of coding. – Blckknght Aug 26 '13 at 14:28
  • I just wanted to show a very simple example of OO and inheritance and this clearly demonstrates those principles(at least imho) .. in reality the classes would be much more – Joran Beasley Aug 26 '13 at 14:38
9

Assuming you want to keep things as simple as possible (no OO) and want to avoid introducing the global keyword, you could instead use a state dictionary and assign variables in there.

state['hasKey'] = True

Since access to this dict is not a variable assignment you avoid introducing the global keyword and at the same time can teach how to use a dict (checking for keys etc.)

Of course you still use a global variable and don't really address the issue of good coding style. But on the other hand, it could serve as an introduction to scopes and namespaces.

Michael Mauderer
  • 3,777
  • 1
  • 22
  • 49