-3
# A dictionary for the simplified dragon text game
# The dictionary links a room to other rooms.
rooms = {
    'Great Hall': {'South': 'Bedroom'},
    'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
    'Cellar': {'West': 'Bedroom'}
}

directions = ['north', 'south', 'east', 'west']
current_room = rooms['Great Hall']

# game loop
while True:
    if current_room == 'Great Hall':
        print('Congratulations! You have reached the Great Hall and defeated the Dragon!')
        break
    print('You are in {}.'.format(current_room))

    command = input('What do you do?')

    if command in directions:
        if command in current_room:
            current_room = rooms[current_room[command]]
        else:
            # bad movement
            print('You cannot go that way.')
    # Quit game
    elif command == 'quit':
        print('Thanks for playing!')
        break
    # bad command
    else:
        print('invalid input')

When I enter in input of directions is keeps telling me that its invalid input ? I also tried entering in the room that i would liek to go in but it keeps giving me invalid input

  • Your code is case sensitive. Do you pay attention to this? – Ertugrul Sungur Oct 07 '22 at 22:22
  • Step through your code with a debugger and look at your variables. For example, if you enter `sorth` then `if command in directions` is `True`, but `if command in current_room` is `False`, because `'South'` is not equal to `'south'` (case). – Mark Tolonen Oct 07 '22 at 22:23
  • So i fixed the capitalization and it still is doing the same thing giving me a invalid input – Dakota Almeida Oct 07 '22 at 22:34
  • You are in {'South': 'Bedroom'}. What do you do? south invalid input You are in {'South': 'Bedroom'}. What do you do? bedroom invalid input You are in {'South': 'Bedroom'}. What do you do? Bedroom invalid input You are in {'South': 'Bedroom'}. What do you do? Great Hall invalid input You are in {'South': 'Bedroom'}. What do you do? North invalid input You are in {'South': 'Bedroom'}. What do you do? South invalid input You are in {'South': 'Bedroom'}. What do you do? East invalid input You are in {'South': 'Bedroom'}. What do you do? West invalid input – Dakota Almeida Oct 07 '22 at 22:35
  • 1
    Yes, that's because the list `['north', 'south', 'east', 'west']` does not contain any of the things that you are trying to type in. If you try typing `north`, for example, it will instead say `You cannot go that way.`, because `north` isn't either of the keys of `{'North': 'Great Hall', 'East': 'Cellar'}`. – Karl Knechtel Oct 07 '22 at 22:56
  • 1
    "So i fixed the capitalization and it still is doing the same thing giving me a invalid input" Please do not try to show code or results in the comments. If you tried to change the code to fix a problem, and the problem is **the same**, [edit] the question to show the new code **instead** (don't just add it). If the problem is **different**, that is a **new question**. – Karl Knechtel Oct 07 '22 at 22:57
  • I can see an unrelated problem you might as well address. You assigned `current_room` to a value from `rooms[]`, which maps room names to another map (map direction to next room name). In the `while` loop you compare `current_room` to the room name `'Great Hall'` which is a string (key), not a map (value), so won't match. – John Bayko Oct 07 '22 at 23:04
  • To avoid worrying about matching strings correctly, look up Python `enum`s. If there's a typo, it'll produce an error, rather than silently fail - that is also the case when you map a direction to a room. Both rooms and directions can be `enum`s. You only have to map the name to the `enum` value on input, and you can convert the case at that point. – John Bayko Oct 07 '22 at 23:10
  • 1
    @Claudio "But there are TWO issues the provided code has" Questions here need to be about one issue in order to stay open. I identified the most obvious issue as a duplicate. The other problem you appear to be pointing at, is that the *initial* value of `current_room` needs to be a string, rather than something looked up from the dictionary. I consider this a typo. Please look at the [tour], [ask] etc. and note that this is *not a discussion forum, nor a help desk*; the purpose of questions here **is not** to debug someone else's code or get it working. – Karl Knechtel Oct 08 '22 at 02:03
  • 1
    I will not discuss the matter here further. Please take it to Meta or to an appropriate chat room, such as https://chat.stackoverflow.com/rooms/197438/the-meta-room. – Karl Knechtel Oct 08 '22 at 03:08

1 Answers1

0

This is because case sensitive means words are not equal if lowercase or uppercase.

  • I fix your code according to the signature of your code in rooms.
    rooms = {
        'Great Hall': {'South': 'Bedroom'},
        'Bedroom': {'North': 'Great Hall', 'East': 'Cellar'},
        'Cellar': {'West': 'Bedroom'}
    }

    directions = ['north', 'south', 'east', 'west']
    current_room = rooms['Great Hall']

    # game loop
    while True:
        if current_room == 'Great Hall':
            print('Congratulations! You have reached the Great Hall and defeated the Dragon!')
            break
        print('You are in {}.'.format(current_room))

        command = input('What do you do?').lower()

        if command in directions:
            if command.capitalize() in current_room:
                current_room = rooms[current_room[command.capitalize()]]
            else:
                # bad movement
                print('You cannot go that way.')
        # Quit game
        elif command == 'quit':
            print('Thanks for playing!')
            break
        # bad command
        else:
            print('invalid input')
  • Your suggested change to the code does not make the game playable because it is not the main issue of the code. – Claudio Oct 07 '22 at 23:16
  • what I understand from your game is its direction which is north, south,... so on And after that you get key_value of current_room of direction that what you implement not me what I did is change the signature of your code to make it choose directions right with ignore case and key_value also. – AbdullahSaidAbdeaaziz Oct 07 '22 at 23:24
  • And also your logic of nested dict not correct. – AbdullahSaidAbdeaaziz Oct 07 '22 at 23:35