【问题标题】:How do I solve a NoneType Error when programming a robot to move?在对机器人进行移动编程时如何解决 NoneType 错误?
【发布时间】:2022-01-25 02:05:20
【问题描述】:

我试图让机器人从一个坐标移动到目标坐标以“收集比萨饼”,然后将比萨饼移动到一组新坐标但是出现以下 NonType Eroor 我不知道这是什么方法。这是我的移动代码:

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]

这是错误信息:

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

这是机器人代码

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

【问题讨论】:

  • “self.coordinates”或“self.target”为无。为了获得更好的答案,需要更多的上下文(代码)。
  • “self.target”在“Robot”初始化代码中设置为None。

标签: python oop


【解决方案1】:

我想,您的robot 对象(coordinatestarget)的属性之一是None,您应该检查一下。

【讨论】:

    【解决方案2】:

    这意味着目标坐标或当前坐标包含 None 值,因为它不允许您使用 self.coordinates[c] 或 self.target[c]。将这两个值都打印出来会让你清楚地理解。

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 2020-08-28
    • 2021-10-11
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    相关资源
    最近更新 更多