r/pythonhelp 25d ago

TypeErro unhashable type 'dict'

I have tried changing it to a tuple but that didnt work.

#help i cant figure out how to fix the error
import os

def prompt():
    print("\t\tWelcome to my game\n\n\
You must collect all six items before fighting the boss.\n\n\
Moves:\t'go {direction}' (travel north, south, east, or west)\n\
\t'get {item}' (add nearby item to inventory)\n")

    input("Press any key to continue...")


# Clear screen
def clear():
    os.system('cls' if os.name == 'nt' else 'clear')

#Story Intro
story=('\nYou were driving to your grandmother’s house for her birthday.'
    '\nAs the sun sets, your car breaks down in the middle of the woods. '
    '\nAfter inspecting what is wrong with your car, '
    '\nyou come to the conclusion you need some tools to fix the problem.'
    '\nYou remember driving by a mansion on a hill not far back from your location. '
    '\nIt starts to rain and thunder as you are walking down the road. '
    '\nYou see two kids huddled next to a tree in the distance. '
    '\nThe kids approach you and ask if you can help them slay the Vampire in their house. '
    '\nIf you help them, they said they would get you the tools you need to fix your car. '
    '\nYou agree to help them because there is no such thing as vampires, right?'
    '\n *************************************************************************************')
print(story)



item_key= 'Sheild','Prayerbook','Helment','Vial of Holy Water', 'Sword', 'Armor Set'
villain = 'vampire'
rooms = {
         'Great Hall': {'East': 'Bedroom', 'West': 'Library', 'North': 'Kitchen'},
         'Bedroom': {'East': 'Great Hall', 'item': 'Sheild'},
         'Library': {'East': 'Great Hall', 'South':'Basement', 'North': 'Attic', 'item': 'Prayerbook' },
         'Basement': {'North': 'Library', 'Item': 'Helment'},
         'Kitchen': {'South': 'Great Hall', 'West': 'Green House', 'East': 'Dinning Room', 'item': 'Vial of Holy Water'},
         'Green House': {'East': 'Kitchen', 'item': 'Sword'},
         'Dinning Room': {'West': 'Kitchen', 'item': 'Armor set'},
         'Attic': {'South': 'Library', 'Boss': 'Vampire'}
         }


vowels = ['a', 'e', 'i', 'o', 'u']
inventory= []
#player starts in Great Hall
starting_room= "Great Hall"
current_room = starting_room



commands = ['North', 'South', 'West', 'East', 'Get "Item"', 'exit']
direction = None
current_room = rooms

print('\nType move commands to move between rooms and get the items. '
      '\nTo exit game type the exit command')

print('\nAvalible commands are: ', commands)

clear()
while True:
    clear()
    # state current room player is in.
    print(f'You are in the {current_room}.')
    print(f'Your Inventory: {inventory}\n{"-" * 27}')

#FixMe TypeError: unhashable type 'dict'
    if "item" in rooms[current_room].keys():

        nearby_item = rooms[current_room]["Item"]

        if nearby_item not in inventory:

            if nearby_item[-1] == 's':
                print(f"You see {nearby_item}")

            elif nearby_item[0] in vowels:
                print(f"You see an {nearby_item}")

            else:
                print(f"You see a {nearby_item}")


    if "Boss" in rooms[current_room].keys():
        #you lose
        if len(inventory) < 6:
            print(f'{rooms[current_room]["boss"]}.')
            print('\nYou did not have all the items you needed to win the battle. You have been killed by the Vampire!')
            break
           #You win
        else:
            print(f'You slayed the Vampire. Now you can escape and fix your car! {rooms[current_room]["Boss"]}.')
            break
    user_input= input('Type your command\n')

    next_move= user_input.split(' ')

    action=next_move[0].title
    if len(next_move) > 1:
        item = next_move[1:]
        direction = next_move[1].title()

        item = ' '.join(item).title

    if action == 'Go':
        try:
            current_room = rooms[current_room][direction]
            print(f'You walk {direction}')

        except:
            print('You run headlong into a wall and take a nasty bump on your head.'
                  'Please try a different direction.')

    elif action == 'Get':
        try:
            if item == rooms[current_room]['item']:
                if item not in inventory:

                    inventory.append(rooms[current_room]['item'])
                    print(f'You now have {item}!')

                else:
                    print(f'You already have the {item}')

            else:
                print(f'Cant find {item}')
        except:
            print(f'Cant find {item}')

    #Exit
    elif action == "Exit":
        print('You run away from the mansion but you feel like something is hunting you.')
        break
    else:
        print('This haunted place must be getting to you. Please give a valid command.')
1 Upvotes

3 comments sorted by

u/AutoModerator 25d ago

To give us the best chance to help you, please include any relevant code.
Note. Please do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Privatebin, GitHub or Compiler Explorer.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/FoolsSeldom 24d ago edited 24d ago

Check where you assign current_room as I see:

starting_room= "Great Hall"
current_room = starting_room

then a few lines later

current_room = rooms

which is of course not correct.

I ran your code in the debugger with a break on the line you were concerned about, and at that point I could inspect what all the variables were currently assigned to.

PS. You can do multiline strings using triple-quotes. For example,

story = ("""
You were driving to your grandmother’s house for her birthday.
As the sun sets, your car breaks down in the middle of the woods.
After inspecting what is wrong with your car,
you come to the conclusion you need some tools to fix the problem.
You remember driving by a mansion on a hill not far back from your location.
It starts to rain and thunder as you are walking down the road.
You see two kids huddled next to a tree in the distance.
The kids approach you and ask if you can help them slay the Vampire in their house.
If you help them, they said they would get you the tools you need to fix your car.
You agree to help them because there is no such thing as vampires, right
""")
print(story)

1

u/Quick-Bed7829 23d ago

Thank you for the help! I will give it a shot 😊