1,105,633 Community Members

Python IF statement issue?

Member Avatar
aj9393
Newbie Poster
4 posts since Oct 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm am trying to make just a simple text-based adventure game, inspired greatly by Zork. Basically all it is is an IF statement for many possible input so the player isn't constrained to inputting their text in a certain format. Then each section is it's own module so when the player selects the option that moves them forward through the game it imports the module of the next section. The issue is with the IF statement. Here's the 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 == 'go east':
        print "You can't get through the trees to the east."
        print " "
        level()

    if choice == 'Go East':
        print "You can't get through the trees to the east."
        print " "
        level()

    if choice == 'Go east':
        print "You can't get through the trees to the east."
        print " "
        level()

    if choice == 'East':
        print "You can't get through the trees to the east."
        print " "
        level()

    if choice == 'east':
        print "You can't get through the trees to the east."
        print " "
        level()

    if choice == 'e':
        print "You can't get through the trees to the east."
        print " "
        level()

    if choice == 'go west':
        print "You can't get through the trees to the west."
        print " "
        level()

    if choice == 'Go West':
        print "You can't get through the trees to the west."
        print " "
        level()

    if choice == 'Go west':
        print "You can't get through the trees to the west."
        print " "
        level()

    if choice == 'West':
        print "You can't get through the trees to the west."
        print " "
        level()

    if choice == 'west':
        print "You can't get through the trees to the west."
        print " "
        level()

    if choice == 'w':
        print "You can't get through the trees to the west."
        print " "
        level()

    if choice == 'go south':
        print "You can't get through the trees to the south."
        print " "
        level()

    if choice == 'Go South':
        print "You can't get through the trees to the south."
        print " "
        level()

    if choice == 'Go south':
        print "You can't get through the trees to the south."
        print " "
        level()

    if choice == 'South':
        print "You can't get through the trees to the south."
        print " "
        level()

    if choice == 'south':
        print "You can't get through the trees to the south."
        print " "
        level()

    if choice == 's':
        print "You can't get through the trees to the south."
        print " "
        level()

    if choice == 'go north':
        import _level1

    if choice == 'Go North':
        import _level1

    if choice == 'Go north':
        import _level1

    if choice == 'North':
        import _level1

    if choice == 'north':
        import _level1

    if choice == 'n':
        import _level1

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

level()

So you're supposed to choose north, it is the correct option. The issue is everything works fine, except when you select north it imports the next section just fine except it also runs the ELSE part of the IF statement. How do I fix this?

Member Avatar
woooee
Posting Maven
2,798 posts since Dec 2006
Reputation Points: 783 [?]
Q&As Helped to Solve: 836 [?]
Skill Endorsements: 12 [?]
 
1
 

The else statement will execute for all input except 'n'. That includes upper case 'N'. You want something more along the lines of the following. Also, use a while statement to call the function multiple times. When level() calls itself you can reach the recursion limit.

choice = choice.lower()
    if choice in ['go south', 'south', 's']:
        print "You can't get through the trees to the south.\n"
        level()

    elif choice in ['go north', 'north', 'n']:
        import _level1
 
    else:
        print "I don't understand."
        print " "
        level()
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: