[Python] How to get item from dictionary and add to list? Getting an item from a room

Discussão em 'Python' iniciado por Stack, Dezembro 20, 2024.

  1. Stack

    Stack Membro Participativo

    def display_instructions():
    # print a main menu and the commands
    print('Welcome to Colossal Cave Adventure Remake')
    print('Move commands: South, North, East, West')
    print("Collect 9 items before encountering the Dragon or be eaten by the Dragon.")
    print("Move commands: South, North, East, West")
    print("To add item to inventory use: Get item")
    display_instructions()

    # The starting room
    current_room = 'Cave Entrance'
    inventory = []
    item_count = 0
    move = ''
    directions = ['North', 'South', 'East', 'West', 'Exit', 'Get Item']
    item_key = 'item'
    villain = 'Dragon'

    # Define room connections
    rooms = {
    'Cave Entrance': {'item': 'No Item', 'South': 'Troll Region', 'North': 'Giants Room', 'East': 'Magnificent Caverns', 'West': 'Pearl Room' },
    'Giants Room': {'item': 'Golden Eggs', 'East': 'Hall of Mists', 'West': 'Forest Region', 'South': 'Cave Entrance'},
    'Hall of Mists': {'item': 'Diamonds', 'West': 'Giants Room'},
    'Forest Region': {'item': 'Magic Staff', 'South': 'Volcano Region', 'East': 'Giants Room'},
    'Volcano Region': {'item': 'Ring of Doom', 'North': 'Forest Region'},
    'Pearl Room': {'item': 'Magic Pearl', 'East': 'Cave Entrance', 'North': 'Pirates Room'},
    'Pirates Room': {'item': 'Pirates Chest', 'South': 'Pearl Room'},
    'Magnificent Caverns': {'South': 'Plover Room', 'West': 'Cave Entrance'},
    'Plover Room': {'item': 'Emeralds', 'North': 'Magnificent Caverns'},
    'Troll Region': {'item': 'Golden Chain', 'North': 'Cave Entrance', 'East': "Villain's Lair"},
    'Villains Lair': {'item': 'Dragon', 'West': 'Troll Region'} # Villains Lair
    }

    def user_status(): # indicate room and inventory contents
    print('\n-------------------------')
    print(f'You are in the {current_room}.')
    print(f'Your inventory is {inventory}.')
    print(f"In this room you see {rooms[current_room]['item']}.")
    print('-------------------------------')


    while move != 'Exit':
    user_status()
    move = str(input('Make a move, Get item or Exit to quit: \n')).title().strip()
    if move not in directions:
    print('Invalid Input. Try Again!')
    continue


    # To get item from room
    if move == 'Get Item':
    if 'item' in rooms[current_room]['item']:
    if 'item' not in inventory:
    inventory.append(rooms[current_room]['item'])
    else:
    print(f"There's no", rooms[current_room]['item'] + " here.")

    #Delete Item From Dictionary
    #del rooms[current_room]['item']
    #if encounter villain in room
    if villain in rooms[current_room].keys():
    # If Lose
    if len(inventory) < 9:
    print(f'You lost fight with {rooms[current_room][villain]}.')
    break
    #Win
    else:
    print(f'You won. You beat {rooms[current_room][villain]}!')
    break
    elif move == 'Exit':
    print("You've exited the game. Bye! Thanks for playing!")

    # If Move Is Right Direction
    elif move in rooms[current_room]:
    current_room = rooms[current_room][move]
    else:
    # If Wrong Direction
    print("You have a wall in the way. Try again")


    When I use the 'get item' the console tells me invalid move. I've tried multiple ways to get item from many different sites but I've had no luck. This was my final attempt.

    I have 'get item' in directions, so it should be a valid move. I have inventory [] also.

    I'm a beginner to Python.

    Continue reading...

Compartilhe esta Página