[Python] Trouble with getting items from rooms

Discussão em 'Python' iniciado por Stack, Abril 18, 2025.

  1. Stack

    Stack Membro Participativo

    I'm trying to write code that lets you get an item from a room and puts it in your inventory, but no matter what I try the function either doesn't execute or gets an error. Here's what I have as of this post:

    room = {
    'Main Entry': {'East': 'Main Floor'},
    'Main Floor': {'North': 'North Hall', 'South': 'South Hall', 'East': 'Clothes Store', 'West': 'Main Entry', 'item': 'Gas Mask'},
    'South Hall': {'East': 'Food Court', 'North': 'Main Floor', 'item': 'Knowledge of Toaster Store'},
    'North Hall': {'East': 'Storage Room', 'South': 'Main Floor', 'item': 'Stuffed Shark'},
    'Food Court': {'West': 'South Hall', 'item': 'Block of Old Cheese'},
    'Clothes Store': {'North': 'Theater', 'West': 'Main Floor', 'item': 'Baseball Bat'},
    'Theater': {'South': 'Clothes Store'}
    }

    item = {
    'Gas Mask',
    'Stuffed Shark',
    'Bubble Wrap',
    'Knowledge of Toaster Store',
    'Block of Old Cheese',
    'Baseball Bat'
    }

    inventory = {

    }

    #The current_room function tracks which "room" the user is in, and starts them in the Main Entry
    current_room = 'Main Entry'
    print('Allister & Doodle enter the mall. The word was, one of their recurring enemies-Lumina the jellyfish lady-was putting' \
    'together a makeshift doomsday device! They figured it was unlikely to do actual doomsday levels of damage, but they had the day' \
    'off and figured they might as well try and put a stop to it. Because they''re just that nice!' \
    'Input Search to look for items! You''ll need Bubble Wrap, a Baseball Bat, and a Block of Old Cheese to win the game.' \
    'Once-and only once-you have all three, make your way to the Theater (North of the Clothes Store) for the final showdown!' \
    'Input North, South, East, or West to have them move through the mall! Or input exit, if you have something better to do.')

    #The while loop plays until the user enters exit, to which it then ends the game.
    while current_room != 'exit':
    print(f"Allister & Doodle are in the {current_room}")
    command = input("Enter command: ")
    if command == 'exit':
    current_room = 'exit'
    if command == 'Search':
    if item in room[current_room]['item']:
    inventory.append(item)
    else:
    print(f"Nothin'.")
    elif command in ['North', 'South', 'East', 'West']:
    direction = command
    if direction in room[current_room]:
    current_room = room[current_room][direction]
    else:
    print("Can't go that way")
    else:
    print("No can do")

    print("You have exited the game.")


    Right now, I'm consistently getting this error:

    Traceback (most recent call last):
    File "c:\Users\name\folder\folder1\folder2\file.py", line 43, in <module>
    if **item in room[current_room]['item']:
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^**
    TypeError: 'in <string>' requires string as left operand, not set


    What do I need to do to fix this?

    Continue reading...

Compartilhe esta Página