6
\$\begingroup\$

I'm working on this little text based game. Up to this point, I've gotten it to work, however I've heard I shouldn't be using global like I am. I have this set up to modify the global variables because I want the player to revisit a place and all the events have still been triggered.

I've just started programming, so I don't have knowledge of lots of concepts. The tutorials I've been reading include Learning Python the Hard Way.

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()  
\$\endgroup\$
  • 3
    \$\begingroup\$ You should probably consider looking into OOP. \$\endgroup\$ – Raz Megrelidze Oct 6 '14 at 23:23
4
\$\begingroup\$

It will make sense to use OOP in your implementation. You can model the crypt as a Crypt class, there gate and lever can be attributes, and entrance, lever_room can be methods.

Transforming your implementation is quite easy, follow this pattern:

class Crypt(object):
    def __init__(self):
        self.gate = False
        self.lever = False

    def entrance(self):
        while True:
            choice = raw_input('> ')

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

# ...

def main():
    crypt = Crypt()
    crypt.welcome()
    crypt.entrance()

To learn more about classes and OOP in Python, I recommend the official tutorial.


It's not a good practice to run code in the global namespace, because if you try to import this module from another module, all that code will be executed immediately.

The right way to do this in Python is to add this conditional:

if __name__ == '__main__':
    main()

This condition will only be true when the file is executed as a Python script.


Don't use the == operator to check the value of boolean variables and expressions like this:

elif choice == 'north' and gate == False:
    print "The gate is still closed and locked."
    entrance()
elif choice == 'north' and gate == True:
    fountain_rm()

Instead of x == True you can use the boolean variable directly, in case of x != False you should use not x, for example:

elif choice == 'north' and not gate:
    print "The gate is still closed and locked."
    entrance()
elif choice == 'north' and gate:
    fountain_rm()

This looks like a bug:

else:
    entrance(gate)

Because in your declaration the entrance method doesn't take any parameters.

\$\endgroup\$
  • \$\begingroup\$ I'll have to look into classes. I haven't gotten to that point in the tutorial, but from looking at your modified code, it seems easy enough. My only question is what is wrong with using the == operator to check the value of a boolean? \$\endgroup\$ – CoopTang Oct 11 '14 at 17:47
  • \$\begingroup\$ Because it's completely unnecessary. Just like you wouldn't write if x and true, of if x or false, when if x is enough and shorter. \$\endgroup\$ – Stop ongoing harm to Monica Oct 11 '14 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.