首页 > 解决方案 > 如何让while循环运行?

问题描述

为了您更好地理解:我尝试为带有列表(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/xerodoc/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()

标签: pythonpython-3.xmultithreadinglistwhile-loop

解决方案


你必须更换

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

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

推荐阅读