owlet
owlet

Reputation: 23

While loop inside a function?

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!!

Upvotes: 1

Views: 1662

Answers (5)

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.

Upvotes: 0

user14637592
user14637592

Reputation:

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()

Upvotes: 0

Keredu
Keredu

Reputation: 388

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()

Upvotes: 0

Hooded 0ne
Hooded 0ne

Reputation: 997

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()

Upvotes: 0

quamrana
quamrana

Reputation: 39354

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()

Upvotes: 1

Related Questions