1

I'm completely new to coding and am working on a text based game as a school project using Python. The user must enter a room by typing 'North', 'East', 'South', or 'West'. If the direction is invalid, an error message should pop up, prompting the user to enter a new direction. If the user types 'Quit', the game should end.

There are a million issues I'm having with this project as I have found I am terrible at coding, but the one I am trying to figure out is how to get my program to quit the game if prompted. Here's my code (it isn't the full code, just what I have so far. I'm trying to figure things out just one step at a time and here's where I am stuck):

rooms = {
    'Great Hall': {'South': 'Bedroom'},
    'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
    'Cellar': {'West': 'Bedroom'}
}


def main():
    current_room = 'Great Hall'
    user_input = None

    while user_input != "Quit":
        print('You are in the', current_room + '.')
        user_input = input('Where would you like to go?: ')
        current_room = rooms[current_room][user_input]
    else:
        print('Thanks for playing!')


main()

When I run the program, I get this error message: Error Message

If anyone can point me in the right direction on what I need to fix, I would be very grateful!!

owlet
  • 23
  • 4
  • 1
    your rooms variable doesn''t contain Quit info. `rooms[current_room]['Quit']` – Pygirl Dec 03 '20 at 17:18
  • Your `while` loop runs until the user's input is `Quit`. However if it's `Quit`, the code will try to access `rooms[current_room]['Quit']`, which doesn't exist. You should just have a `while True` loop, then `break` if the user's input is `Quit`, and only _after_ you check for that, then you'd run `current_room = rooms[current_room][user_input]`. But you might want to check that the user input is a valid key in `rooms`: https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – Random Davis Dec 03 '20 at 17:21

5 Answers5

1

So when your user inputs "Quit", you go ahead and try to find it in rooms. You should move your exit condition to after the input rather than be part of the while condition:

rooms = {
    'Great Hall': {'South': 'Bedroom'},
    'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
    'Cellar': {'West': 'Bedroom'}
}


def main():
    current_room = 'Great Hall'

    while True:
        print('You are in the', current_room + '.')
        user_input = input('Where would you like to go?: ')
        if user_input == "Quit":
            break
        current_room = rooms[current_room][user_input]
    print('Thanks for playing!')

main()
quamrana
  • 37,849
  • 12
  • 53
  • 71
0

Try putting it inside an if statement? Your error is happening because it tries to find the right room to move to before it loops back to the while statement. Using an if immediately forces it to determine quit first.

rooms = {
    'Great Hall': {'South': 'Bedroom'},
    'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
    'Cellar': {'West': 'Bedroom'}
}


def main():
    current_room = 'Great Hall'
    user_input = None

    while user_input != "Quit":
        print('You are in the', current_room + '.')
        user_input = input('Where would you like to go?: ')
        
        if user_input == "Quit":
            print('Thanks for playing!')
        else:
            current_room = rooms[current_room][user_input]
    

main()
Hooded 0ne
  • 881
  • 1
  • 3
  • 10
0

You can use the walrus operator in order to have a simpler code:

rooms = {
    'Great Hall': {'South': 'Bedroom'},
    'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
    'Cellar': {'West': 'Bedroom'}
}

def main():
    current_room = 'Great Hall'

    while user_input:=input('Where would you like to go?: ') != 'Quit':
        print('You are in the ' + current_room + '.')
        current_room = rooms[current_room][user_input]
    print('Thanks for playing!')

main()
Keredu
  • 378
  • 1
  • 14
0

The error is that the Quit key isn´t in the rooms dictionary. I have a version of your code that works:

rooms = {
    'Great Hall': {'South': 'Bedroom'},
    'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
    'Cellar': {'West': 'Bedroom'}
}

def main():
    current_room = 'Great Hall'

    while True:
        print('You are in the', current_room + '.')
        user_input = input('Where would you like to go?: ')
        if user_input == "Quit":
            break
    if user_input in rooms[current_room]:
        current_room = rooms[current_room][user_input]
    print('Thanks for playing!')
main()
Dharman
  • 30,962
  • 25
  • 85
  • 135
0

This is an Improvement of the code for avoid the ErrorKey in dict:

rooms = {
'Great Hall': {'South': 'Bedroom'},
'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
'Cellar': {'West': 'Bedroom'}
}


def main():
    current_room = 'Great Hall'

while True:
    print('You are in the', current_room + '.')
    user_input = input('Where would you like to go?: ')
    if user_input == "Quit":
        break
    possible_directions = rooms[current_room].keys()
    if user_input not in possible_directions:
        print()
        print('You can not go:', user_input)
        print('Try instead', )
        print(possible_directions)
        print()
        continue
    current_room = rooms[current_room][user_input]
print('Thanks for playing!')

main()

A thing to note is that the program starts an endless loop. So if you run it, like me in a Jupyter Notebook and you run the cell for a second time before type 'Quit' Jupyter will still execute the previous process and the new one at the same time.