我想弄清楚为什么我会收到两条错误消息,以及如何解决它们。我正在使用 python

I am trying to figure out why I am getting the two error messages that I am, and how to fix them. I am using python

我遇到了图片错误的问题。它说我的字典 'rooms' 甚至没有被引用。我不确定该怎么做或如何解决这个问题。我对字典没有太多经验。我仍在学习 python 并且正在为这段代码苦苦挣扎。我老老实实的要拔头发了

# Define player location and state current inventory
def player_location():
    print('-' * 20)
    print('You are in the {}'.format(location))
    print('Inventory: ', inventory)
    print('-' * 20)

def get_new_location(location, player_move):
    new_state = location
    if location in rooms:
        if player_move in rooms[location]:
            new_state = rooms[location][player_move]

    return new_state

def get_room_item(location):
    return rooms[location]['Item']

# Function to show instructions and welcome players to the game.
def show_instructions():
    print("Welcome to the Saga of Light Text Adventure Game!")
    print("Collect 6 items to win the game, or be beaten by The Dark Elf Nebo.")
    print("Movement commands: North, South, East, West, or Exit to leave the Game.")
    print("Add to Inventory: Get 'Item'")


def main_gameplay():
    # Dictionary linking rooms and items obtained in them.
    rooms = {
    'Main Hall': {'South': 'Bedroom', 'North': 'library', 'East': 'Kitchen', 'West': 
    'Music Room'},
    'Music room': {'East': 'Main Hall', 'Item': 'Music Box'},
    'Bedroom': {'North': 'Main Hall', 'East': 'Safe room', 'Item': 'Cloak of Strength'},
    'Safe room': {'West': 'Bedroom', 'Item': 'Bow & Arrows'},
    'Dining Room': {'South': 'Kitchen', 'Item': 'The Dark Elf Nebo'},
    'Kitchen': {'West': 'Main Hall', 'North': 'Dining Room', 'Item': 'Energy Drink'},
    'Study': {'West': 'Library', 'Item': 'Ring of Light'},
    'Library': {'East': 'Study', 'South': 'Main Hall', 'Item': 'Book'}
}

rooms = main_gameplay()
# Call for the function to display instructions
show_instructions()

# variables for the current room and player moves and empty inventory list.
location = 'Great Hall'
player_move = ''
inventory = []

# gameplay loop for game
while len(inventory) < 6:
    player_location()
    player_move = input('Which direction would you like to go?\n').title()  # Ask player 
                for direction to go.
    if player_move == 'Exit':  # If Exit is selected, game over.
        location = 'Exit'
        print('Thank you for playing!')  # Thank you message for playing
    if 'Item' in rooms[get_new_location(location, player_move)]:
        print('You see a ', 'Item')
        if player_move == ('Get Item'):
            inventory = inventory.append()
    else:  # using if/else statements and dict for rooms and telling players where they 
            are.
        location = get_new_location(location, player_move)
        print(location)
        if location in rooms:
            if location == 'Main Hall':
                print()
            elif location == 'Safe Room':
                print()
            elif location == 'Bedroom':
                print()
        else:
            print("Move not valid. Please try again.")  # Error message for invalid 
                                                          direction.
        if len(inventory)==6:
            print('Congratulations! You have collected all items and defeated the 
                  dragon!') 
    exit(0)
    main_gameplay()

我收到的错误:

line 61, in <module>
if 'Item' in rooms[get_new_location(location, player_move)]:
NameError: name 'rooms' is not defined

rooms = main_gameplay() 将函数 main_gameplay return 的任何内容分配给变量 rooms。当您在函数 main_gameplay 中创建变量 rooms 时,函数 main_gameplay 不会 return 任何东西,换句话说,它隐式 returns Nonerooms 仅在函数 main_gameplay 的局部范围内)。因此,在全局范围内,变量 roomsNone。这就是为什么您收到错误消息 TypeError: argument of type 'NoneType' is not iterable. 只需在函数声明 main_gameplay.

的末尾添加 return rooms