0

嗨,我正在努力用这个代码让我的头绕着缩进水平 -

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)

有人可以建议缩进在哪里不正确吗?

史蒂夫

4

5 回答 5

4

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

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)
于 2013-08-09T07:18:50.263 回答
0
if count > 10:
break
else:
return(temperature)

if-else 条件也需要缩进。它应该是

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,它指定文件只能以读取模式打开。

于 2013-08-09T07:18:43.967 回答
0

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
于 2013-08-09T07:19:16.093 回答
0

if语句存在缩进问题:

if count > 10:
    break
else:
    return(temperature)

一个

于 2013-08-09T07:20:32.490 回答
-1
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

应该只有4个,为什么要放8个空格?试试看!:)

编辑:if 语句,if: 下的任何内容都应该缩进 4 个空格!与 elif 和 else 相同!:)

于 2013-08-09T07:20:01.330 回答