mardi 25 février 2020

Python: Split() method in input alter "if" statements outcome in text based RPG game

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 add a split() method, then the conditions

elif "open door" == choice:
elif "search door" == choice:

aren't met anymore.

I get

print "Invalid"

for "open door" == choice

and

print "You find nothing."

for "search door" == choice

Basically this means it jumps to the else: statements.

I'd like to use the split() method so that if the appropriate input is given but with added text attached to the conditional word, for example "kegaasoaosij" instead of "keg" the condition isn't met.

I'd ideally add a dictionary file so that search with nonsensical words don't put out:

print "You find nothing."

But this is for another session...

Any ideas on to how to tackle this problem ? Any other improvements are welcomed.

Thanks for the help !

prison_key = False
def test():
    global prison_key
    curr_prompt = "What do you do?"
    print curr_prompt
    choice = raw_input("> ").lower().split()
    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 "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 "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().split()
    exit(0)

test()

Aucun commentaire:

Enregistrer un commentaire