【问题标题】:How to get the while loop running?如何让while循环运行?
【发布时间】:2020-03-29 08:59:44
【问题描述】:

为了您更好的理解: 我尝试用一​​个列表(intersectionList)为汽车创建一个交叉路口控制,如果汽车想要穿过十字路口,每辆车都会在其中写下他的地址。

如果路段 20 或 23 上的汽车将其地址写入列表,并且如果其条目的索引为 0,则允许它们通过。一旦他们穿过交叉路口(piece10),他们就会从列表中删除他们的条目。我的问题现在与 while 循环有关。汽车做我想做的一切,但在他们的列表索引从 >=1 更改为 == 0 后不适应他们的速度。这意味着在现实生活中,他们认识到他们的索引不是 0,停止,但比他们永远停止......所以我认为while循环有问题。

class Logic:

    intersectionList = []

    def logic(self, addr, piece):
        if piece == 20:
            self.intersectionList.append(addr)

            while self.intersectionList.index(addr) >= 1: #as long index of list is not 0
                self.car.changeSpeed(0, 1000)  #stop car
            else:
                self.car.changeSpeed(300, 1000) #start car

        elif piece == 23: 
            self.intersectionList.append(addr)

            while self.intersectionList.index(addr) >= 1:
                self.car.changeSpeed(0, 1000) #stop car
            else:
                self.car.changeSpeed(300, 1000) #start car

        elif piece == 10:
           if addr in self.intersectionList:
               self.intersectionList.remove(addr) 

我们开始吧。 Overdrive 类是官方 ANKI Overdrive SDK 的 python 包装器,这里上传太长了。 github链接:https://github.com/xerodotc/overdrive-python.git

from overdrive import Overdrive


class Logic:


    intersectionList = []

    def  __init__(self, macAddress):
        car = Overdrive(macAddress)



    def locationChangeCallback(self, addr, location, piece, speed, clockwise):
        self.logic(addr, piece)

    def logic(self, addr, piece):

        if piece == 20:
            self.intersectionList.append(addr)

            while self.intersectionList.index(addr) >= 1:  # as long index of list is not 0
                self.car.changeSpeed(0, 1000)  # stop car

            else:
                self.car.changeSpeed(300, 1000)  # start car


        elif piece == 23:
            self.intersectionList.append(addr)

            while self.intersectionList.index(addr) >= 1:
                self.car.changeSpeed(0, 1000)  # stop car

            else:
                self.car.changeSpeed(300, 1000)  # start car

        elif piece == 10:
            if addr in self.intersectionList:
             self.intersectionList.remove(addr)





    def startEngine(self):
        self.car.setLocationChangeCallback(self.locationChangeCallback)
        self.car.changeSpeed(300,1000)


# instances

bmw = Logic("CD:DF:4R:53:34:D3")
bmw.startEngine()

lambo = Logic("CD:DF:4R:53:34:D3")
lambo.startEngine()

【问题讨论】:

  • 我不知道是不是这个原因,但是intersection_list 是一个class(如静态)变量,这不太可能是你想要的。
  • 嗨,这是全班逻辑吗?如果是这样,变量 car 是在哪里初始化或传递的?
  • 不,我想要那样。每辆车都有一个逻辑对象,我想要,所有对象只有一个列表。我是 python 新手,但我在互联网上查找,他们说,它是这样完成的。即使我打印出列表,一切看起来都很好
  • 不,不是。你们需要吗?
  • okk okk 我的另一个问题是 changeSpeed 的两个参数是什么?

标签: python python-3.x multithreading list while-loop


【解决方案1】:

你必须更换

while self.interSectionList.index(addr) >= 1:

while self.interSectionList.index(addr) >= 0:

【讨论】:

  • 不,我不这么认为。我希望汽车的速度保持 =0,只要它们的 listIndex 不为 0。
【解决方案2】:

这有点令人难以置信:

        self.intersectionList.append(addr)

        while self.intersectionList.index(addr) >= 1: #as long index of list is not 0
            self.car.changeSpeed(0, 1000)  #stop car

除非在 changeSpeed 中完成,否则索引不会更改。因此,要回答您的问题,了解一下 changeSpeed 会很方便。

【讨论】:

  • 我不明白.. 让我们举个例子:两辆车在赛道上,这两辆车都想穿过交叉路口。因此,他们都将地址放入列表中,并且第一个放入其地址的地址允许驱动,因为索引 == 0。一旦它通过第 10 条轨道,列表条目就会被删除,并且另一辆车的条目移动到索引 0。现在允许这辆车穿过十字路口
  • 我明白你想要做什么。您是否可以上传一段工作代码,以便读者了解您的函数在其上下文中是如何工作的?
猜你喜欢
  • 1970-01-01
  • 2020-03-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-29
  • 2016-10-10
  • 1970-01-01
  • 2022-10-09
  • 2019-06-09
相关资源
最近更新 更多