'NoneType'对象没有属性'sense'

时间:2015-12-22 15:57:29

标签: python

landmarks = [[20.0,20.0],[80.0,80.0],[20.0,80.0],[80.0,20.0]]
world_size = 20.0

class robot:
   def __init__(self):
      self.x=random.random()*world_size
      self.y=random.random()*world_size
      self.orientation=random.random()*2.0*pi
      self.forward_noise= 0.0
      self.turn_noise=0.0
      self.sense_noise=0.0

  def set(self,new_x,new_y,new_orientation):
      self.x= float(new_x)
      self.y= float(new_y)
      self.orientaion=float(new_orientation)

  def move(self,turn,distance_move):
      self.orientation += turn%(2*pi)
      self.x += distance_move*(cos(self.orientaion))
      self.y += distance_move*(sin(self.orientaion))

  def sense(self):
     z=[]
     for i in range(len(landmarks)):
         x = sqrt((self.x - landmarks[i][0])**2 + (self.y-landmarks[i][1])**2)
         z.append(x)
         return z

myrobot= robot() 
myrobot.set(30.0,50.0,pi/2)
myrobot=myrobot.move((-pi)/2,15.0)
print(myrobot.sense())  
myrobot=myrobot.move((-pi)/2,10.0)
print(myrobot.sense())

我收到错误

Traceback (most recent call last):
  File "C:/Users/pc/AppData/Local/Programs/Python/Python35-32/particle robot.py", line 36, in <module>
    print(myrobot.sense())
AttributeError: 'NoneType' object has no attribute 'sense'.

代码有什么问题

1 个答案:

答案 0 :(得分:3)

您已将myrobot变量替换为robot.move()方法的返回值:

myrobot=myrobot.move((-pi)/2,10.0)

由于myrobot.move()返回None(如果函数中没有return语句,则为默认值),您的下一行会中断。

不要指定返回值,只能调用move()

myrobot.move((-pi)/2,10.0)