0

I've been learning how to code for a month now using the "Learn Python The Hard Way" tutorial. So far it's been a lot of fun. I challenged myself in creating a text based RPG game.

I am currently re-writing the code for improved readability and maintenance.

I am facing a problem:

If I type in the choice input "open heaby door" (notice the typo mistake), the conditions are still met and I get the expected print as if it didn't have the typo mistake.

Any ideas on what I've done wrong?

Thanks for the help :)

PS: I have another question but I figured I'd have to ask on a separate question file.

prison_key = False
def test():
    global prison_key
    curr_prompt = "What do you do?"
    print curr_prompt
    choice = raw_input("> ").lower()
    while "quit" not in choice:
        if "go" in choice:
            if "cellar" in choice:
                print "cellar"
            elif "gravel" in choice or "path" in choice:
                print "gravel path"
            elif "prison" in choice and prison_key:
                print "You enter the prison."
            elif "prison" in choice: 
                print "The door is locked."
            else:
                print "Invalid"
        elif "search" in choice:
            if "search" == choice:
                print "invalid"
            elif "prison":
                print "The door is closed."
            elif "bucket" in choice:
               print "The bucket is empty."
            elif "keg" in choice:
                prison_key = True
                print "You find a key."
            elif "door" in choice and ("heavy" in choice or "steel" in choice or "metal" in choice):
                print "It looks like a prison door"
            elif "search door" == choice:
                print "Which one?"
            else:
                print "You find nothing."
        elif "open door" == choice:
                print "which one?" 
        elif "open" in choice:
            if "door" in choice and ("wooden" in choice or "cellar" in choice):
                print "gravel path"
            elif "door" in choice and ("steel" in choice or "metal" in choice or "door" in choice or "heavy" in choice or "prison" in choice) and prison_key:
                print "You open the prison"
            elif "door" in choice and ("steel" in choice or "metal" in choice or "door" in choice or "heavy" in choice or "prison" in choice):
                print "the door is locked"
            elif "prison" in choice and prison_key:
                print "You enter the prison."
            elif "prison" in choice:
                print "the door is locked."
            else:
                print "invalid"
        elif "drink" in choice and "wine" in choice:
            print "You alcoholic."
        else:
            print "invalid"
        print curr_prompt
        choice = raw_input("> ").lower()
    exit(0)

test()
gpiriou
  • 1
  • 3

2 Answers2

1

so found your problem. In the elif condition for

elif "door" in choice and ("steel" in choice or "metal" in choice or "door" in choice or "heavy" in choice or "prison" in choice):

You are checking for door again, and thus it passes the if condition. Same applies for this condition also

elif "door" in choice and ("steel" in choice or "metal" in choice or "door" in choice or "heavy" in choice or "prison" in choice) and prison_key:                 

    print "the door is locked"
Anurag Reddy
  • 1,159
  • 11
  • 19
0

I think the mistake is that you put "door" in choice in the second aprt of your and clause:

elif "door" in choice and ("steel" in choice or "metal" in choice or "door" in choice or "heavy" in choice or "prison" in choice) and prison_key:
            print "You open the prison"

The same applies for the next elif.

Nice project btw.

Thomas Schillaci
  • 2,348
  • 1
  • 7
  • 19
  • Oh my gosh !! DUH ! Sorry for this post I should've taken a quick break and re-read my code carefully before posting ! Thanks a lot Anurag and Thomas for the input anyways ! – gpiriou Feb 25 '20 at 16:07