【问题标题】:Function calls for text based game基于文本的游戏的函数调用
【发布时间】:2022-01-13 01:49:48
【问题描述】:

我是一个初学者,尝试编写一个简单的基于文本的游戏,用户在不同的房间之间移动,从每个房间中挑选一个物品,并以遇到老板结束。事情似乎进展顺利,除非我尝试在我的主函数中调用函数 status()。我不断收到未定义变量的错误。任何帮助将不胜感激。

# Create a function for the opening menu
def show_menu():
    print('You are about to play a text based game.\n')
    print('The goal of the game is to collect all seven items before encountering the ghost.\n')
    print('These items contain information about the ghost that can finally put them to rest.\n')
    print('If you approach the ghost without all of the items you will be possessed and the game will end.\n')
    print('The commands are: move North, move South, move East, move West, menu, and quit.\n')
    print('When you find an item, to add it to your inventory enter: get Item')


# Create a function that outputs current room, current room inventory and users current inventory
def status():
    print('You are currently in the: {}.'.format(current_room))
    print('The item currently in this room is {}.'.format(rooms[current_room]['item]']))
    print('Your current inventory is: {}.'.format(inventory))
    control = input('What would you like to do? move North, move South, move East, move West, menu, or quit.')
    return control


def movement_error():
    print('You look around and realize that you wanted to move through a wall, try a different direction.')
    control = input('What would you like to do? move North, move South, move East, move West, menu, or quit.')
    return control


def main():
    # Create Dictionary linking rooms and items
    rooms = {
        'Foyer': {'East': 'Office', 'South': 'Cafeteria', 'item': 'Newspaper Article'},
        'Office': {'West': 'Foyer', 'item': 'Student Report'},
        'Cafeteria': {'North': 'Foyer', 'West': 'English Classroom', 'South': 'Math Classroom', 'East': 'Band Room',
                      'item': 'Crumpled Paper'},
        'English Classroom': {'East': 'Cafeteria', 'item': 'Student Journal'},
        'Math Classroom': {'East': 'Girls Bathroom', 'item': 'Itinerary'},
        'Girls Bathroom': {'West': 'Math Classroom', 'item': 'Notebook'},
        'Band Room': {'East': 'Cafeteria', 'North': 'Basement', 'item': 'Picture'},
        'Basement': {'South': 'Band Room'},  # Ghost Room
    }

    directions = ['North', 'East', 'South', 'West']
    control = ''
    current_room = 'Foyer'
    while control != 'exit':
        inventory = []
        while current_room != ['Basement']:
            status()
            if control in directions and contol in current_room:
                current_room = rooms[current_room][control]

【问题讨论】:

    标签: python variables function-call


    【解决方案1】:

    这些名称仅在 main 中可见。将它们传递给您的函数:

    def status(rooms, current_room, inventory):
        # ...
    

    使用:

    control = status(rooms, current_room, inventory)
    

    注意分配给control。目前,您的status函数返回的值是未使用的。

    【讨论】:

      【解决方案2】:

      您也可以在主函数中将变量声明为global

      def main():
          global current_room
          global inventory
          global rooms
          ...
      

      【讨论】:

        猜你喜欢
        • 2021-06-25
        • 1970-01-01
        • 2013-07-08
        • 2015-09-10
        • 2023-01-08
        • 1970-01-01
        • 1970-01-01
        • 2022-01-24
        • 1970-01-01
        相关资源
        最近更新 更多