0

I thought it would be cool to make a text-based adventure game. I am having a problem with a naming error in this block of code:

 elif room1_choice1=='Search the room':
        print('You have chosen to search the room')
        print("As you walk around the room you find a chest that you couldn't see from the doorway")
        print('You see only two choices, to attempt to open it or to search the rest of the room')
        print("Type 'Attempt to open' to try to open the chest or type 'Keep searching' to continue your search")
        room1_chestChoice=input(':')
 if room1_chestChoice=='Attempt to open':
        print('You heave at the lid with all your strength but fail to open it.')
        print('It looks like it is locked and you need a key')
        print("Type 'Keep searching' to try and find the key")
        room1_chestChoice=input(':')

I get an error at the if in the middle:

Traceback (most recent call last):
  File "C:/Users/maxim/PycharmProjects/APCSP project/Prototype 1.py", line 41, in <module>
    if room1_chestChoice=='Attempt to open':
NameError: name 'room1_chestChoice' is not defined

Can anyone help?

1
  • 3
    shouldn't it be room1_choice1 ? Commented Mar 27, 2018 at 17:48

1 Answer 1

2

room1_chestChoice is defined only if the previous choice, room1_choice1, was "search the room". Checking room1_chestChoice makes sense only in that case. Change your indentation to reflect your decision tree:

elif room1_choice1=='Search the room':
    print('You have chosen to search the room')
    ...
    room1_chestChoice=input(':')
    if room1_chestChoice=='Attempt to open':
         print('You heave at the lid
               ...
         room1_chestChoice=input(':')

That second if has to be entirely within the code dependent on the elif.

Sign up to request clarification or add additional context in comments.

Comments

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.