0

I'd like to start out that I'm new to programming. I've never taken any classes on it. I just decided it sounded interesting and try it.

Anyway, I've been reading the "Learn Python the Hard Way" online and have gotten to exercise 36. This exercise involves making my own text-based game. Now, for the question. When is an appropriate time to use and modify global variables? I just started my adventure and want to add things that the player has to do before other events happen, such as pull a lever in a different room before the gate in the first room opens. And if the player wishes to return to the first room later on, the gate and lever still be triggered.

Here's the code so far. Mind you it's not fleshed out. Just wanted to know if it worked.

print "You are a lone adventurer with the bounty to clear out the crypt."
print "You come with nothing but your sword and a compass."
print "You enter the crypt."
print "To the north you have a gated portaculas and rooms to the west and east."
print "What do you do?"

gate = False
lever = False
def entrance():
    global gate

    while True:
        choice = raw_input('> ')

        if choice == 'west':
            guard_shack()
        elif choice == 'east':
            lever_rm()
        elif choice == 'north' and gate == False:
            print "The gate is still closed and locked."
            entrance()
        elif choice == 'north' and gate == True:
            fountain_rm()
        else:
            entrance(gate)

def lever_rm():
    global lever
    global gate

    print "You enter the room."
    print "What do you do"

    while True:
        choice = raw_input('> ')

        if 'search' in choice:
            print "You look around the room and notice a lever on the opposite wall."
        elif "pull lever" in choice:
            print "You pull the lever."
            lever = True
            gate = True
        elif choice == 'west':
            entrance()
        else:
            print '> '

def fountain_rm():
    print "you're in the lever room!"
entrance()  
Huangism
  • 16,278
  • 7
  • 48
  • 74
CoopTang
  • 129
  • 1
  • 1
  • 5
  • 2
    If this code works, it might have a better fit on http://codereview.stackexchange.com/ – SethMMorton Oct 02 '14 at 02:00
  • possible duplicate of [Are global variables bad?](http://stackoverflow.com/questions/484635/are-global-variables-bad) – simonzack Oct 02 '14 at 02:19
  • 1
    While you're at it, reconsider calling entrance() recursively from an endless loop! – Ulrich Eckhardt Oct 02 '14 at 05:50
  • If you're getting stuck on globals, I suggest [you read this Reddit post and my answer](http://www.reddit.com/r/learnpython/comments/2brlza/classes_and_self_im_not_saying_that_im_lost_but/), where I help someone extract globals into state that gets passed around. – Veedrac Oct 02 '14 at 07:37

1 Answers1

0

Unfortunately, many tutorials (and professors) teach bad code in the name of simplicity (and they usually never bother to teach the right way later). In this case, the problem is exacerbated by the fact that you are directly executing top-level code instead of putting it in a main function and using if __name__ == '__main__': main() at the end.

You should try to avoid all global state that can be mutated. It's okay to declare constants, or even lists/sets/dicts that you're not allowed to change. But everything else should be either passed as a function parameter, or stored as an attribute on self of some class.

If nothing else, think about how you would write unit tests in the presence of mutable global variables (hint: it's impossible).

To transform code like you've given, indent everything and add a heading class CryptGame:, add self as the first argument to every function, and replace all variables declared global foo with self.foo.

o11c
  • 15,265
  • 4
  • 50
  • 75
  • I've yet to get into classes, so I'm not sure how they work or why. Also, I don't know about the main function. What makes mine top-level code instead of using the main function? – CoopTang Oct 05 '14 at 18:46