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 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()
gpiriou
  • 1
  • 3

2 Answers2

0

You don't really need to split the string. The operation a in b will check if substring a is in the main string b. Hope it helps

EDIT: hi PGlenn, thank you for being so kind! I've put together this piece of code:

choices = choices.split()
for i in range(len(choices)-1):
    if choices[i] + ' ' + choices[i+1] in ('open door', 'close door'):
        print(choices[i] + ' ' + choices[i+1])
        newval = choices[i] + ' ' + choices[i+1]
        choices.remove(choices[i])
        choices.remove(choices[i])
        choices.insert(i, newval)
        break

This loop basically searches for 'open door' and for 'close door' and updates the choices list accordingly. Unfortunately it doesn't work if there are multiple 'open doors' and 'close doors' in the same string. Hope it helps and good luck with your Python journey!

PMM
  • 366
  • 1
  • 10
  • 1
    Thanks for the input Pietro! Although it doesn't seem to be what I'm looking for. Maybe I got your answer wrong but what happens is: If I put "search kegsojaosijfoifjs" or whatever subsequent letters attached to the keyword "keg" in the input, the condition is met. I would like this not to happen and maybe the split() method isn't the right one, it seems to be working. But: With the split, it isn't met anymore, which is good, but then the "open door" == choice and "search door" == choice statements aren't met. Which is not what I'm looking for. Maybe I've got this wrong. – gpiriou Feb 25 '20 at 17:42
  • Allright, I'll try it out tomorrow ! Thanks again Pietro, I appreciate it. – gpiriou Feb 26 '20 at 00:48
0

Ok everyone I finally found a solution to my problem.

Since the split() method will cut the STRING input "open door" in elements within a LIST, the best solution is to have a list as a conditional statement in order for the: "Which one?" statements to be met.

So:

if ["open", "door"] == choice
    print "Which one?"

and

if ["search", "door"] == choice
    print "Which one?"

This is what I was looking for and is the best solution for the effect I had in mind.

This prevents the user from typing the searched keywords with additional undesired characters attached to it.

Thanks to all participants for their help ! Much appreciated :)

gpiriou
  • 1
  • 3