[Python] Text based game: Get item [closed]

Discussão em 'Python' iniciado por Stack, Fevereiro 20, 2025.

  1. Stack

    Stack Membro Participativo

    I am creating a text based game for my Python 140 class. One of the requirements is that the user can "get" an item from each room and add it to his inventory. When I run the code and attempt to "get" the item from that room, it errors with a traceback for line 56 saying "name 'lower' is not defined'". What am I doing incorrectly to cause the game not to add the item to inventory?

    print("Welcome! To defeat HUNGER, you must collect all 6 SATIATING FOODS.")

    name = input("What is your name? ")

    rooms = {
    'FOREST': {'North': 'PUB', 'East': 'FARM', 'West': 'FIELD'},
    'PUB': {'North': 'HOUSE', 'East': 'CELLAR', 'South': 'FOREST', 'West': 'RESTAURANT', 'item': 'FISH AND CHIPS'
    },
    'FARM': {'North': 'FARMHOUSE', 'West': 'FIELD',
    'item': 'POTATOES'
    },
    'FIELD': {'North': 'RESTAURANT', 'East': 'FOREST',
    'item': 'BERRY MEDLEY'
    },
    'RESTAURANT': {'East': 'PUB', 'South': 'FIELD',
    'item': 'STIR FRY'
    },
    'FARMHOUSE': {'North': 'CELLAR', 'South': 'FARM',
    'item': 'HEARTY STEW'
    },
    'CELLAR': {'South': 'FARMHOUSE', 'West': "PUB",
    'item': 'CANNED SOUP'
    },
    'HOUSE': {'South': "PUB",
    'item': 'HUNGER'
    },

    }

    directions = ['North', 'South', 'East', 'West']
    location = 'FOREST'
    item = ['POTATOES', 'BERRY MEDLEY', 'STIR FRY', 'FISH AND CHIPS', 'HEARTY STEW', 'CANNED SOUP']
    inventory = []

    print("Welcome to Trials of Harvest."
    "You can go North, East, South, or West. Type 'Exit' to leave the game.")

    while True: # Gameplay loop
    print(f'You are in the {location}, {name}.')
    print("Inventory:", inventory)
    if rooms['item']:
    print('Item in room: {}'.format(''.join(rooms['item'])))
    print('------------------------------')

    direction = input("Type North, East, South, West, or Exit: ").capitalize()

    if direction in rooms[location]: # Check if the direction exists in the current room
    location = rooms[location][direction] # Move to the new room
    elif direction == "Exit":
    print(f'Thanks for playing, {name}!')
    break
    elif lower().split()[0] == 'get':
    item = " ".join(command.lower().split()[1:])
    if item in rooms['item']:
    Inventory.append(item)
    print('You grabbed {}.'.format(rooms['item']))
    else:
    print("I don't see that here.")
    else:
    print('You cannot go that way!')

    while currentRoom in ['HUNGER']:
    if len(Inventory) == 6:
    print('You collected all of the items, and defeated HUNGER!')
    else:
    print('It looks like you have not found everything, you lose!')
    break

    Continue reading...

Compartilhe esta Página