Python对象方法将自我参数视为缺失

时间:2015-02-24 23:27:26

标签: python-3.x

我正在制作一个基于文本的游戏来自学python。我尝试访问存储在播放器对象中的对象的属性。该对象是地图数据对象的副本,表示它们在地图上的位置。

当我按原样运行函数时,当调用player对象的look方法时,该方法将self参数视为缺少参数。如果我尝试访问存储在播放器对象中的房间数据并将其传入(这似乎不正确,因为它应该已经存在于对象实例中),然后它说该属性不存在。程序崩溃后,在解释器中我可以无错误地访问该属性。我不明白我做错了什么。

class quadPoint:

    def __init__(self, data):

    self.pointData = {'description' : '', 'west': '','east': '',
            'north': '','south': ''}
    self.exits = []
    self.inventory = []

    self.pointData['description'] = data[0]
    self.pointData['west'] = data[1]
    self.pointData['east'] = data[2]
    self.pointData['north'] = data[3]
    self.pointData['south'] = data[4]

    def addItem(self, item):
    self.inventory.append(item)

    def addExit(self, ext):
    self.exits.append(ext)

rooms = [
    ['A discription1','A discription','A discription','A discription','A discription'],
    ['A discription2','A discription','A discription','A discription','A discription'],
    ['A discription3','A discription','A discription','A discription','A discription'],
    ['A discription4','A discription','A discription','A discription','A discription'],
    ['A discription5','A discription','A discription','A discription','A discription'],
    ['A discription6','A discription','A discription','A discription','A discription'],
    ['A discription7','A discription','A discription','A discription','A discription'],
    ['A discription8','A discription','A discription','A discription','A discription'],
    ['A discription9','A discription','A discription','A discription','A discription']
    ]

roomExits = {
    '00':[ [0,1],[1,0] ], # 0,0
    '01':[ [0,0],[0,2],[1,1] ],  # 0,1
    '02':[ [0,1],[1,2] ], # 0,2
    '10':[ [0,0],[1,1],[2,0] ], # 1,0
    '11':[ [1,0],[1,2],[0,1],[2,1] ], # 1,1
    '12':[ [1,1],[2,2] ], # 1,2
    '20':[ [1,0],[2,1] ], # 2,0
    '21':[ [2,0],[1,1],[2,2] ], # 2,1
    '22':[ [2,1],[1,2] ], # 2,2
    }

class World :

    def __init__(self, data, exits):
    self.worldData = [ [],[],[] ]
    self.generate(data, exits)

    def getRoom(self,x,y):
    return self.worldData[x][y]


    def generate(self, data, exits):
    ph = 0
    for array in self.worldData:
       for i in range(0,3):
           array.append('')

    for x in range(0,3):
        for i in range(0,3):
            self.worldData[x][i] = quadPoint(data[ph])
            ph += 1

    for quards in exits:
        for e in exits[quards]:
            self.worldData[int(quards[0])][int(quards[1])].addExit(self.worldData[e[0]][e[1]])


class Player :


    def __init__(self, room):

    self.statData = {}


    self.equipment = { 'head' : '', 'neck' : '', 'leftEar' : '',
              'rightEar' : '', 'leftShoulder' : '',
              'rightShoulder' : '', 'chest' : '',
              'leftUpperArm' : '', 'rightUpperArm' : '',
              'leftLowerArm' : '', 'rightLowerArm' : '',
              'leftFinger0' : '','leftFinger1' : '',
              'leftFinger2' : '','leftFinger3' : '',
              'leftFinger4' : '','rightFinger0' : '',
              'rightFinger1' : '','rightFinger2' : '',
              'rightFinger3' : '','rightFinger4' : '',
              'leftWrist' : '','rightWrist' : '',
              'upperBack' : '','lowerBack' : '','back' : '','Waist' : '',
              'leftUpperLeg' : '','rightUpperLeg' : '',
              'leftLowerLeg' : '','rightLowerLeg' : '',
              'leftAnkle' : '','rightAnkle' : '',
              'leftFoot' : '','rightFoot' : ''}

    print("What would you like your name to be?")
    print()
    self.name = input(">>> ")
    print('''What would you like your name to be
    Wizard
    Fighter
    Thief
    ''')
    self.plrClass = input(">>> ")
    self.currentRoom = room


    def look(self, where):
        return self.currentRoom.pointData[where]




class Game :

    def __init__(self):
    self.world = World(rooms, roomExits)
    self.player = Player(self.world.getRoom(0,0))

    def start(self):
    self.running = True
    while self.running is True:
        print(player.look( 'description'))
        print()
        print("What do you want to do?")
        print()
        usrInput = input(">>> ")

        if userInput == 'quit': self.running = False


game = Game()

game.start()

错误信息是:

Traceback (most recent call last):
  File "/home/groundscore/pytextrpg.py", line 128, in <module>
    game.start()
  File "/home/groundscore/pytextrpg.py", line 117, in start
    print(player.look( 'description'))
TypeError: look() takes exactly 2 arguments (1 given)

2 个答案:

答案 0 :(得分:4)

aruisdante打败我直接回答。更深层次的问题,以及特定错误消息的原因是对类和实例使用相同的小写字。如果播放器类是播放器,那么print(player...)将失败NameError: name 'player' is not defined,您将看不到因player.look被解析为look属性导致的混乱消息玩家类。

此外,game = game()game = Game()更令人困惑,并阻止通过其名称进一步访问游戏类。

答案 1 :(得分:3)

我不确定您是否正确描述了您的错误(如果您包含确切的错误文本,而不是您对其的描述会很有帮助),但肯定在这里:

print(player.look( 'description'))

应改为:

print(self.player.look( 'description'))

由于playergame类的属性。此外,您应该使用is进行字符串比较,您应该使用==