嗨,我正绞尽脑汁想用这个代码把我的脑袋弄圆-
def getSolarFlowtemperature():
#Open the temperature sensor, read it and process the result
tfile = open("/sys/bus/w1/devices/28-000003086819/w1_slave")
text = tfile.read()
tfile.close()
temperature_data = text.split()[-1]
temperature = float(temperature_data[2:])
temperature = temperature / 1000
#This while function checks for the error temperatures, and tries to read the sensor again to get a proper value. After 10 tries it stops
count = 0
while temperature == -0.062 or temperature == -0.125:
time.sleep(2)
count = count + 1
print 'Temperature error on 28-000003086819, retrying'
tfile = open("/sys/bus/w1/devices/28-000003086819/w1_slave")
text = tfile.read()
tfile.close()
temperature_data = text.split()[-1]
temperature = float(temperature_data[2:])
temperature = temperature / 1000
if count > 10:
break
else:
return(temperature)
有人能告诉我压痕哪里不正确吗?你知道吗
史蒂夫
while块缩进应该是这样的:
如果不是,条件也需要缩进。应该是的
其他注意事项:
return temperature
不需要像您这样的括号。你知道吗此外,要打开文件、读取文件并关闭文件,只需执行以下操作:
这样可以确保即使在出现异常的情况下也关闭文件句柄。此外,我还传递了第二个参数
r
,该参数指定只应在读取模式下打开文件。你知道吗if/else应该在while中,它们的代码应该缩进,中断在循环之外没有意义
相关问题 更多 >
编程相关推荐