gpt4 book ai didi

python : While True or False

转载 作者:太空宇宙 更新时间:2023-11-03 12:47:54 25 4
gpt4 key购买 nike

我不是经验丰富的程序员,我的代码有问题,我认为这是我的逻辑错误,但我在 http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/whilestatements.html 找不到答案。 .我想要的是检查串口设备是否被锁定,“锁定”和“未锁定”的区别在于有4个逗号 ,,,,包含 GPGGA 字母的行。所以我希望我的代码在没有 ,,,, 的情况下启动,但我想我的循环是错误的。任何建议将不胜感激。提前致谢。

import serial
import time
import subprocess


file = open("/home/pi/allofthedatacollected.csv", "w") #"w" will be "a" later
file.write('\n')
while True:
ser = serial.Serial("/dev/ttyUSB0", 4800, timeout =1)
checking = ser.readline();
if checking.find(",,,,"):
print "not locked yet"
True
else:
False
print "locked and loaded"

...

最佳答案

使用break 退出循环:

while True:
ser = serial.Serial("/dev/ttyUSB0", 4800, timeout =1)
checking = ser.readline();
if checking.find(",,,,"):
print "not locked yet"
else:
print "locked and loaded"
break

TrueFalse 行在您的代码中没有做任何事情;它们只是引用内置的 boolean 值,而没有在任何地方分配它们。

关于 python : While True or False,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25484269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com