[英]Python : While True or False
I am not an experienced programmer, I have a problem with my code, I think it's a logical mistake of mine but I couldn't find an answer at http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/whilestatements.html .我不是一个有经验的程序员,我的代码有问题,我认为这是我的逻辑错误,但我在http://anh.cs.luc.edu/python/hands-on/找不到答案3.1/handsonHtml/whilestatements.html 。 What I want is to check if the serial device is locked, and the different between conditions that "it is locked" and "it isn't locked" is that there are 4 commas ,,,,
in the line which contains GPGGA
letters.我想要的是检查串口设备是否被锁定,“它被锁定”和“它没有被锁定”的条件之间的区别是在包含GPGGA
字母的行中有4个逗号,,,,
。 So I want my code to start if there isn't ,,,,
but I guess my loop is wrong.所以我希望我的代码在没有,,,,
启动,,,,
但我想我的循环是错误的。 Any suggestions will be appreciated.任何建议将不胜感激。 Thanks in advance.提前致谢。
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"
. . . . . .
Use break
to exit a loop:使用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
The True
and False
line didn't do anything in your code; True
和False
行在您的代码中没有做任何事情; they are just referencing the built-in boolean values without assigning them anywhere.他们只是引用内置的布尔值,而没有在任何地方分配它们。
You can use a variable as condition for your while
loop instead of just while True
.您可以使用变量作为while
循环的条件, while
不仅仅是while True
。 That way you can change the condition.这样你就可以改变条件。
So instead of having this code:因此,不要使用此代码:
while True:
...
if ...:
True
else:
False
... try this: ... 尝试这个:
keepGoing = True
while keepGoing:
ser = serial.Serial("/dev/ttyUSB0", 4800, timeout =1)
checking = ser.readline();
if checking.find(",,,,"):
print "not locked yet"
keepGoing = True
else:
keepGoing = False
print "locked and loaded"
EDIT:编辑:
Or as another answerer suggests, you can just break
out of the loop :)或作为另一个回答者指出,你可以break
循环了:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.