Python 文本冒险游戏收到AttributeError

Python 文本冒险游戏收到AttributeError,python,python-3.x,Python,Python 3.x,所以我正在用Python 3开发一个文本冒险游戏,我不明白的是def available\u actions(self):应该如何工作 下面的文件是我的gamedata函数,我在其中处理位置、项目和世界: class Location: def __init__(self, brief_description, full_description, available_actions): '''Creates a new location. ADD N

所以我正在用Python 3开发一个文本冒险游戏,我不明白的是
def available\u actions(self):
应该如何工作

下面的文件是我的gamedata函数,我在其中处理位置、项目和世界:

class Location:

    def __init__(self, brief_description, full_description, available_actions):
    '''Creates a new location.          
    ADD NEW ATTRIBUTES TO THIS CLASS HERE TO STORE DATA FOR EACH LOCATION.

    Data that could be associated with each Location object:
    a position in the world map,
    a brief description,
    a long description,
    a list of available commands/directions to move,
    items that are available in the location,
    and whether or not the location has been visited before.
    Store these as you see fit.

    This is just a suggested starter class for Location.
    You may change/add parameters and the data available for each Location class as you see fit.

    The only thing you must NOT change is the name of this class: Location.
    All locations in your game MUST be represented as an instance of this class.
    '''
        self.get_brief_description = brief_description
        self.get_full_description = full_description
        self.available_actions = available_actions

    def get_brief_description (self):
    '''Return str brief description of location.'''

        return self.brief_description

    def get_full_description (self):
    '''Return str long description of location.'''

        return self.full_description

    def available_actions(self):
    '''
    -- Suggested Method (You may remove/modify/rename this as you like) --
    Return list of the available actions in this location.
    The list of actions should depend on the items available in the location
    and the x,y position of this location on the world map.'''

        return self.available_actions
这是下面的第二个文件,名为
adventure.py
,我应该在其中处理游戏本身的程序

import gamedata


if __name__ == "__main__":
    WORLD = World("map.txt", "locations.txt", "items.txt")
    PLAYER = Player(0,0) # set starting location of player; you may change the x, y coordinates here as appropriate

    menu = ["look", "inventory", "score", "quit", "back"]

    while not PLAYER.victory:
        location = WORLD.get_location(PLAYER.x, PLAYER.y)

    # ENTER CODE HERE TO PRINT LOCATION DESCRIPTION
    # depending on whether or not it's been visited before,
    #   print either full description (first time visit) or brief description (every subsequent visit)

        print("What to do? \n")
        print("[menu]")
        for action in location.available_actions():
            print(action)
        choice = input("\nEnter action: ")

        if (choice == "[menu]"):
            print("Menu Options: \n")
            for option in menu:
                print(option)
            choice = input("\nChoose action: ")
当我运行文件
adventure.py
时,我得到一个错误


AttributeError: 'NoneType' object has no attribute 'available_actions'```

This error would be on this line:

    for action in location.available_actions():

Is the reason why I get this error because of the 

    def available_actions(self):
        '''
        -- Suggested Method (You may remove/modify/rename this as you like) --
        Return list of the available actions in this location.
        The list of actions should depend on the items available in the location
        and the x,y position of this location on the world map.'''

            return self.available_actions

In the gamedata function? I'm unsure of how to go about this part of the code and how to fix this error.

看起来您的世界级应用程序应该创建接收可用选项菜单的位置对象。您的位置构造函数可能应该指定“available_actions”不是空的,因为这会在代码后面导致错误。我可以想象,在这种游戏中,如果你在一个没有可用动作的位置,游戏就会停止。

问题就在这里

location = WORLD.get_location(PLAYER.x, PLAYER.y)

因为在这一行中,变量“location”被赋值,错误告诉您它是一个“NoneType”对象。在类的
get\u location
世界中搜索问题,并确保返回正确的内容。

可用的\u操作方法定义与在adventure.py中使用它的方式不同


您不会将self传递给每个方法,它是通过实例变量隐含的。传递self将向方法添加一个变量,如果代码中没有对该变量进行寻址,将导致上述错误,因为签名不匹配。只需将self从Location.py中的每个方法中删除即可。错误在
World
类中
WORLD.get_location(PLAYER.x,PLAYER.y)
不(总是)返回
位置。它(有时)返回
None
@user38034,当我运行它时,我在location.available\u actions():AttributeError:'NoneType'对象没有属性'available\u actions'