Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python缩进新手_Python_Indentation - Fatal编程技术网

Python缩进新手

Python缩进新手,python,indentation,Python,Indentation,嗨,我正绞尽脑汁想用这段代码把我的脑袋弄圆- 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 =

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

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)
有人能告诉我压痕哪里不正确吗

史蒂夫

如果其他条件也需要缩进。应该是

if count > 10:
  break
else :
  return temperature
其他一些注意事项:

返回温度
不需要像您这样的括号

此外,要打开、读取和关闭文件,您可以执行以下操作:

with open("/sys/bus/w1/devices/28-000003086819/w1_slave", "r") as tfile :
  text = tfile.read()
这样可以确保即使在出现异常的情况下也关闭文件句柄。此外,我还传递了第二个参数
r
,该参数指定只应在读取模式下打开文件

如果其他条件也需要缩进。应该是

if count > 10:
  break
else :
  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)

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)

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

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
应该只有4个,为什么要放8个空格?试试看!:)

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

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


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

if语句中存在缩进问题:

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

Aif语句中存在缩进问题:

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

A

只读参数是不必要的,因为这是默认模式。谢谢你,我已经做了你建议的更改。我认为打开、读取和关闭文件的建议代码的第2行应该缩进,对吗?是的,应该缩进只读参数是不必要的,因为这是默认模式。谢谢你,我已经做了你建议的更改。我认为打开、读取和关闭文件的建议代码的第二行应该缩进,对吗?是的,应该缩进1空格缩进是profs使用的!不,说真的,python可以有任意数量的空格缩进,就像您和您的工作环境一样。想要两个?使用2,想要8?使用8。没什么问题。1空格缩进是profs使用的!不,说真的,python可以有任意数量的空格缩进,就像您和您的工作环境一样。想要两个?使用2,想要8?使用8。没什么问题。谢谢你,如果你能给我更多的建议,我会很感激的。这是我作为一个新手为自己使用而提升和修改的代码:)其思想是函数尝试10次来检索我相信的值。Break,将打破循环结构并停止迭代,因此它必须以某种方式嵌套在循环中。在这里,我们看到if在while中的中断,因此中断指的是while。如果还有一个循环,它就会从内部循环中中断。使用中断循环上下文是不可能的,也没有任何意义,因为没有迭代可以停止,试试看,你会得到一个错误:)谢谢你的帮助,希望你能提供更多关于中断的建议,无论它在哪里都没有意义。这是我作为一个新手为自己使用而提升和修改的代码:)其思想是函数尝试10次来检索我相信的值。Break,将打破循环结构并停止迭代,因此它必须以某种方式嵌套在循环中。在这里,我们看到if在while中的中断,因此中断指的是while。如果还有一个循环,它就会从内部循环中中断。使用中断循环上下文是不可能的,也没有任何意义,因为没有迭代可以停止,尝试一下您将得到一个错误:)