python - 如何让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()
解决方案
你必须更换
while self.interSectionList.index(addr) >= 1:
和
while self.interSectionList.index(addr) >= 0:
推荐阅读
- uiviewcontroller - 是否可以在某些视图控制器上使用自动布局并在其他视图控制器上使用自动调整大小?(斯威夫特 4)
- react-native - 如何使导航栏(标题)颜色与反应导航中的正文相同?
- html - 转换比例时避免网格重叠
- python - 我是否缺少基于 random.sample 的命令的某些内容
- c# - 如何使用包含 Web 服务的库,而无需在使用应用程序的配置文件(例如 app.config)中定义端点?
- selenium-webdriver - 使用量角器在 MicrosoftEdgeDrive 中发送特殊键时特殊键变成键盘编号
- reactjs - 尝试导入错误:“DeviceEventEmitter”未从“react-native”导出
- awk - 编译出了点问题
- java - Spring-Boot @PreAuthorize 仅允许对管理员进行操作,或者如果经过身份验证的用户 id 与路径参数 id 相同
- php - 警告:imagedestroy() 期望参数 1 是资源