1

I am writing a program that is sort of a mini text adventure game however I seem to be experiencing some issues with my code. I am relatively new to programming as a whole so if you could explain your answers in-depth it would be of great assistance.

Here's the code:

#text_adventure_game.py
#python_ver == 3.5

verbs = ["get",
         "put",
         "drop",
         "use",
         "enter",
         "leave",
         "walk",
         "search",
         "attack",
         "defend"]

nouns = ["journal",
         "magnifier",
         "glasses",
         "knife",
         "kite",
         "table",
         "chair",
         "key"]

inventory = ""

user_input = input("What would you like to do?\n")

#checking if there are a set of two instructions
if(len(user_input.split())==2):
    counter = 0
    for token in user_input.split():
        print(token)
        print(counter)
        if(counter==0):
            if(token not in verbs):
                print("I did not understand the verb entered.")
                #removing square brackets and single quote marks from the array
                print("Please use:", ", ".join(verbs))
                counter = counter + 1
            elif(token in verbs):
                print("Recognized verb entered.")
                counter = counter + 1
        if(counter==1):
            if(token not in nouns):
                print("I did not understand the noun entered.")
                print("Please use:", ", ".join(nouns))
                counter = counter + 1
            elif(token in nouns):
                print("Recognized verb entered.")
                counter = counter + 1

My problem is that it does not recognize the nouns I enter in the "nouns" array.

This is how it compiles:

>>> 
What would you like to do?
get journal
get
0
Recognized verb entered.
I did not understand the noun entered.
Please use: journal, magnifier, glasses, knife, kite, table, chair, key
journal
2

If there are more efficient ways of doing things like this then that would also help.

Thanks.

4 Answers 4

1

There's no need for a for loop and a counter. You can more simply do:

verbs = ["get",
         "put",
         "drop",
         "use",
         "enter",
         "leave",
         "walk",
         "search",
         "attack",
         "defend"]

nouns = ["journal",
         "magnifier",
         "glasses",
         "knife",
         "kite",
         "table",
         "chair",
         "key"]

inventory = ""

user_input = input("What would you like to do?\n")

#checking if there are a set of two instructions
action, obj = user_input.split()

if(action not in verbs):
    print("I did not understand the verb entered.")
    print("Please use:", ", ".join(verbs))
else:
    print("Recognized verb entered.")

    if(obj not in nouns):
        print("I did not understand the noun entered.")
        print("Please use:", ", ".join(nouns))
    else:
        print("Recognized noun entered.")

If you want to make it repeat after failure, you can put the code under a while block.

Anyway the problem in your original code is that you have to insert a continue if the first verb is correct:

verbs = ["get",
         "put",
         "drop",
         "use",
         "enter",
         "leave",
         "walk",
         "search",
         "attack",
         "defend"]

nouns = ["journal",
         "magnifier",
         "glasses",
         "knife",
         "kite",
         "table",
         "chair",
         "key"]

inventory = ""

user_input = input("What would you like to do?\n")

#checking if there are a set of two instructions
if(len(user_input.split())==2):
    counter = 0
    for token in user_input.split():
        print("1", token)
        print(counter)
        if(counter==0):
            if(token not in verbs):
                print("I did not understand the verb entered.")
                #removing square brackets and single quote marks from the array
                print("Please use:", ", ".join(verbs))
                counter = counter + 1
            elif(token in verbs):
                print("Recognized verb entered.")
                counter = counter + 1
                continue
        if(counter==1):
            if(token not in nouns):
                print("I did not understand the noun entered.")
                print("Please use:", ", ".join(nouns))
                counter = counter + 1
            elif(token in nouns):
                print("Recognized verb entered.")
                counter = counter + 1
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for pointing out the error, now I know how to use continue. I like your simplified solution also. Thanks a lot for your help.
0

You increment the counter when you find the verb, so the line if(counter==1) executes before the token is incremented (token still contains 'get' while the counter==1 block is executing).

The simplest fix would be to make the

if(counter==1):

into an elif:

elif(counter==1):

Or if you would like to be nice and explicit, you could add a 'continue' each time you have successfully processed a token, and the loop will move onto the next iteration without processing any more of the loop body:

if(counter==0):
    if(token in verbs):
        print("Recognized verb entered.")            
    else
        print("I did not understand the verb entered.")
        #removing square brackets and single quote marks from the array
        print("Please use:", ", ".join(verbs))

    counter = counter + 1
    continue

(Note, I have also changed your if / elif test, as you are testing two mutually exclusive boolean conditions - 'in' and 'not in'.)

Hope that helps.

2 Comments

Thanks for the help, I'll use continue for when I have successfully processed a token. This seems like a nice solution.
You're very welcome. Feel free to accept one of the answers that has helped the most, as that person will then get some reputation points.
0

The problem is that you increase the counter variable in "if(counter==0):" from 0 to 1. Since you have another if statement right afterwards ("if(counter==1):") both if statements will execute during the same iteration which means that with an input of "get key" you would check if "get" is in verbs but also if "get" is in nouns. I'd propose changing the second if to an elif like this:

verbs = ["get",
         "put",
         "drop",
         "use",
         "enter",
         "leave",
         "walk",
         "search",
         "attack",
         "defend"]

nouns = ["journal",
         "magnifier",
         "glasses",
         "knife",
         "kite",
         "table",
         "chair",
         "key"]

inventory = ""

user_input = input("What would you like to do?\n")

#checking if there are a set of two instructions
if(len(user_input.split())==2):
    counter = 0
    for token in user_input.split():
        print(token)
        print(counter)
        if(counter==0):
            if(token not in verbs):
                print("I did not understand the verb entered.")
                #removing square brackets and single quote marks from the array
                print("Please use:", ", ".join(verbs))
                counter += 1
            else: #this can be simplified as an else since the booleans you check are mutually exclusive
                print("Recognized verb entered.")
                counter += 1
        elif(counter==1): #this now only executes if counter isn't 1 to make sure it only triggers on the second word in the array
            if(token not in nouns):
                print("I did not understand the noun entered.")
                print("Please use:", ", ".join(nouns))
                counter += 1
            else:
                print("Recognized noun entered.")
                counter += 1

1 Comment

Ah, I see, I probably should of spotted this earlier. Thanks for pointing this out.
0

The real problem is the position of your count. Here is the code changed. Of course, you could also use a continue on the first if counter == 0, you can event change the if counter == 1 to an elif counter == 1.

#text_adventure_game.py
#python_ver == 3.5

verbs = ["get",
         "put",
         "drop",
         "use",
         "enter",
         "leave",
         "walk",
         "search",
         "attack",
         "defend"]

nouns = ["journal",
         "magnifier",
         "glasses",
         "knife",
         "kite",
         "table",
         "chair",
         "key"]

inventory = ""

user_input = input("What would you like to do?\n")

#checking if there are a set of two instructions
if(len(user_input.split())==2):
    counter = 0
    for token in user_input.split():
        print(token)
        print(counter)
        if(counter==0):
            if(token not in verbs):
                print("I did not understand the verb entered.")
                #removing square brackets and single quote marks from the array
                print("Please use:", ", ".join(verbs))
                counter = counter + 1
            elif(token in verbs):
                print("Recognized verb entered.")
        if(counter==1):
            if(token not in nouns):
                print("I did not understand the noun entered.")
                print("Please use:", ", ".join(nouns))
                counter = counter + 1
            elif(token in nouns):
                print("Recognized noun entered.")
        counter = counter + 1

1 Comment

Nice and simple solution to my problem, thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.