-4

I have attempted changing the rooms around and for some reason I always get an output of You cant go that way, please choose a new direction!, or Invalid entry. I have not been able to move rooms. What should am I doing wrong? I dont know what I am doing and people are so rude.

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

place = 'Great Hall'


def go_new_place(place, direction):
    new_place = place
    for i in rooms:
        if i == place:
            if direction in rooms[i]:
                new_place = rooms[i][direction]

    return new_place


while 1:
    print('You are in', place)
    direction = input('Which direction would you like to go? Or do you wish to exit?')
    if direction == 'Exit':
        exit(0)

    if direction == 'North' or 'South' or 'West' or 'East':
        new_place = go_new_place(place, direction)

        if new_place == place:
            print('You cant go that way, please choose a new direction!')
        else:
            place = new_place
    else:
        print('Invalid entry!')
ked730
  • 3
  • 2
  • Copy and paste the actual code, don't take a photo of it. – Samwise Aug 07 '21 at 20:18
  • See [How do I format my code blocks?](https://meta.stackoverflow.com/q/251361/3890632) and also [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/q/285551/3890632) – khelwood Aug 07 '21 at 20:18
  • Im sorry for uploading a photo. I am very new to this and just needed some help. – ked730 Aug 07 '21 at 20:21
  • 2
    This doesn't appear to be the problem you're asking about, but see [Why does `a == x or y or z` always evaluate to True?](https://stackoverflow.com/q/20002503/3890632) – khelwood Aug 07 '21 at 20:24
  • A definite improvement would be to have: `if direction in ['North', 'South', 'West', 'East']:` – quamrana Aug 07 '21 at 20:33
  • I took your advice and fixed the directions, thank you! – ked730 Aug 07 '21 at 20:40

1 Answers1

1

I just run your code and made some tests with inputs and the code work fine. Maybe when you input a direction you use lowercase characters? There is a difference between South and south in python.

Edit: To get rid of the spaces the user may input, you can use the strip() function. it takes a string and returns it without the spaces in the start or the end of the string. Very useful.

Eladtopaz
  • 1,036
  • 1
  • 6
  • 21
  • EDIT: I found my issue. I was adding a space before inputting my direction. I appreciate you! Thank you! The code is working, however the rooms are supposed to change based off the direction I am entering. I have tried entering both upper and lower case and I get the same answer. – ked730 Aug 07 '21 at 20:26
  • I just run the program, when I have input `South` the room changed to `Bedroom`. When I input `East` the room changed to `Cellar`. Then I input `West` and the room change to `Bedroom` again. Everything worked to me... Of course it doesn't work when you use `North` in the `Great Hall` or `South` in the `Cellar` because it isn't an option in the dict. – Eladtopaz Aug 07 '21 at 20:29
  • I will attempt to add the strip() function. Thank you! – ked730 Aug 07 '21 at 20:37
  • Glad to help :) – Eladtopaz Aug 07 '21 at 20:38