当将 return 放入 while 循环时,循环将停止 如何修复它?
ser = serial.Serial(
port='COM5',
baudrate = 9600,
timeout=1)
while 1:
x=str(ser.readline())
x = re.findall("\d+\.\d+", x)
x = float(x[0])
return(x) #loop stopped
print(x)
请你帮助我好吗?
只需带上您的
x=str(ser.readline())
x = re.findall("\d+\.\d+", x)
x = float(x[0])
return(x) #loop stopped
把它放入像这样的函数中
def foo(ser):
x=str(ser.readline())
x = re.findall("\d+\.\d+", x)
x = float(x[0])
return(x)
并将你的 while 循环更改为简单的
while 1:
print(foo(ser))
然而@developius有一个更好的解决方案,看起来像
while 1:
x=str(ser.readline())
x = re.findall("\d+\.\d+", x)
x = float(x[0])
print(x)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)