I can't figure out what the problem is or how to rewrite my code to fix it. Basically I'm trying to create an Adventure game heavily influenced by the game Zork. The game presents you with some options and then you input your choice. I'm trying to account for a couple different possible user inputs so that your aren't confined to having to type everything a certain way. Then each new section is a separate module that is imported when you make the correct choice. Here's my code:

print "Welcome Adventurer! What is your name?"
print " " 

name = raw_input(">")

print "Hello, " + name + ".Let's begin."
print "You find yourself in the middle of a thick forest. There are trees"
print "surrounding you to the east, south, and west."
print " "

def level():
    choice = raw_input(">")

    if choice in ['go south', 'Go south', 'Go South', 's', 'S', 'south', 'South']:
        print "You can't get through the trees to the south."
        print " "
        level()

    if choice in ['go west', 'Go west', 'Go West', 'w', 'W', 'west', 'West']:
        print "You can't get through the trees to the west."
        print " "
        level()

    if choice in ['go east', 'Go east', 'Go East', 'e', 'E', 'east', 'East']:
        print "You can't get through the trees to the east."
        print " "
        level()

    elif choice in ['go north', 'Go north', 'Go North', 'n', 'N', 'north', 'North']:
        print " "
        import _level1

    else:
        print "I don't understand."
        print " "
        level()

level()

In this case the next section is a module named _level1, as of now I haven't written the code for _level1, it's just a Hello World program just so I could see how the code works. So, in this case north is the correct selection. Now, when you type north on the first try it imports _level1 correctly, but if you type in one of the wrong selections first (east, west, south) it performs properly until you type north at which point it imports _level1 but it also runs the ELSE part of the IF statement. How do I fix this?

I have not ran the code above, but I believe this is because the 'else' is only connected to the 'go east' choice not the if statements above it. Try making 'west' and 'east' elif statements.

Also you could make them all if statements, remove the else and push in the print("i dont understand") one indentation level.

Usually import statements are at the beginning of the program, and you then call a function within the imported program.

import _level1


    elif choice in ['go north', 'Go north', 'Go North', 'n', 'N', 'north', 'North']:
        print " "
        _level1.function_to_run()
This article has been dead for over six months. Start a new discussion instead.