Python问题:TypeError:' NoneType'对象不可调用

时间:2018-04-05 04:32:40

标签: python

运行python xxx.py时出现以下错误消息。如何解决,非常感谢。

  

c:\ Python27> python xxx.py   Traceback(最近一次调用最后一次):     文件" test.py",第34行,in       打印播放器(1,0,10)   TypeError:' NoneType'对象不可调用

以下是xxx.py:

def create(pos=[0,0]):
    def player(dir_x,dir_y,step):
        new_x = pos[0] + dir_x*step 
        new_y = pos[1] + dir_y*step
        pos[0] = new_x  
        pos[1] = new_y
        return pos
return player

if __name__ == '__main__':  
player = create()
    print player(1,0,10)
    print player(0,1,20)
    print player(-1,0,10) 

1 个答案:

答案 0 :(得分:1)

除了缩进之外,您的代码没有任何问题。正确的是:

def create(pos=[0,0]):
    def player(dir_x,dir_y,step):
        new_x = pos[0] + dir_x*step 
        new_y = pos[1] + dir_y*step
        pos[0] = new_x  
        pos[1] = new_y
        return pos
    return player

if __name__ == '__main__':  
    player = create()
    print player(1,0,10)
    print player(0,1,20)
    print player(-1,0,10)

打印:

[10, 0]
[10, 20]
[0, 20]