Python缩进newbi

2024-04-19 02:09:00 发布

您现在位置:Python中文网/ 问答频道 /正文

嗨,我正绞尽脑汁想用这个代码把我的脑袋弄圆-

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)

有人能告诉我压痕哪里不正确吗?你知道吗

史蒂夫


Tags: andthetextreaddatacountsysit
3条回答

while块缩进应该是这样的:

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)

如果不是,条件也需要缩进。应该是的

if count > 10:
  break
else :
  return temperature

其他注意事项:

return temperature不需要像您这样的括号。你知道吗

此外,要打开文件、读取文件并关闭文件,只需执行以下操作:

with open("/sys/bus/w1/devices/28-000003086819/w1_slave", "r") as tfile :
  text = tfile.read()

这样可以确保即使在出现异常的情况下也关闭文件句柄。此外,我还传递了第二个参数r,该参数指定只应在读取模式下打开文件。你知道吗

if/else应该在while中,它们的代码应该缩进,中断在循环之外没有意义

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)

相关问题 更多 >