Home > Blockchain >  How do I solve a NoneType Error when programming a robot to move?
How do I solve a NoneType Error when programming a robot to move?

Time:12-24

I'm trying to get a robot to move from one coordinate to a target coordinate to "collect a pizza" then move with the pizz to a new set of coordinates however the following NonType Eroor comes up and i dont know what this means. here is my move code:

def move(self):
    for c in [0,1]:
      if (self.coordinates[c] < self.target[c]):
        if ((self.target[c] - self.coordinates[c]) < self.max[c]):
          self.velocity[c] = (self.target[c] - self.coordinates[c])
        else: 
          self.velocity[c] = self.max[c]
      elif (self.coordinates[c] > self.target[c]):
        if ((self.coordinates - self.target[c]) < self.max[c]):
          self.velocity[c] = -(self.coordinates[c] - self.target[c])
        else: 
          self.velocity[c] = -self.max[c]
      else:
        self.velocity[c] = 0
      self.coordinates[c]  = self.velocity[c]

and here is the error message:

TypeError                                 Traceback (most recent call last)
<ipython-input-140-00c07d25f390> in <module>()
     33       robot.activity = 'idle'
     34 
---> 35   robot.move()
     36   ecosystem.update()
     37   print(ecosystem.hour)

<ipython-input-136-c31d02f30645> in move(self)
     36   def move(self):
     37     for c in [0,1]:
---> 38       if (self.coordinates[c] < self.target[c]):
     39         if ((self.target[c] - self.coordinates[c]) < self.max[c]):
     40           self.velocity[c] = (self.target[c] - self.coordinates[c])

TypeError: 'NoneType' object is not subscriptable

this is the robot code

class Robot: 
  model = 'robot1'
  def __init__(self): 
    self.coordinates = [1,2,0]           
    self.kind = 'Robot'
    self.max = [1,1,0]
    self.velocity = [1,1,0]
    self.status = 'on'
    self.name = 'robotJL'
    self.activity = 'idle'
    self.target = None
    self.age = 0
    self.serviced = 0
    self.soc = 600
    self.capacity = 600
    self.service = 0
    self.size = 500
    self.damage = 0
    self.color = 'green'
    self.shape = 'triangle'
    self.on_arena = 0
    self.service = 0
    self.weight = 50
    self.payload = 100
    self.cargo = None
    self.distance = 0
    self.energy = 0

CodePudding user response:

I suppose, one of the properties of your robot object (coordinates or target) is None, you should check it.

CodePudding user response:

It means that target coordinates or present coordinates contain None value because of that it is not allowing you to use either self.coordinates[c] or self.target[c]. Printing both the values out will give you clear understanding.

  • Related