2016-12-31 156 views
-3

我能够成功运行代码,但希望能够永久循环它。它是我第一次尝试Python,任何帮助,将不胜感激。我所做的是在代码的顶部放置了“While True:”,但我一直收到“Sorry Indentation Error:expected a indenting block line 3”的错误,但没有它,它工作正常,但只运行一次。所以我的问题是我怎样才能循环而不会出现错误?为什么我不能在第一行写上“While True:”?Noob查询Python(树莓派)

While True: 
# Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before. 
tfile = open("/sys/bus/w1/devices/10-000802824e58/w1_slave") 
# Read all of the text in the file. 
text = tfile.read() 
# Close the file now that the text has been read. 
tfile.close() 
# Split the text with new lines (\n) and select the second line. 
secondline = text.split("\n")[1] 
# Split the line into words, referring to the spaces, and select the 10th word (counting from 0). 
temperaturedata = secondline.split(" ")[9] 
# The first two characters are "t=", so get rid of those and convert the temperature from a string to a number. 
temperature = float(temperaturedata[2:]) 
# Put the decimal point in the right place and display it. 
temperature = temperature/1000 
import time 
print temperature 
time.sleep(5) 
+3

好像你需要读一个Python的教程,它可以解释语法的基础知识这个样子。在不知道缩进所起作用的情况下,用Python进行编程是不可能的。 – Barmar

+1

''while'的'w'应该是小写 –

回答

1

您需要在while True:语句下面的所有代码中添加四个空格。 Python以块为单位工作,while语句开始一个块,这意味着块之后的所有代码都有4个空格。此外,导入语句应该位于顶部,因为Python只需要导入一次代码,而不是多次导入代码。该代码应该是这样的:

import time 

while True: 
    # Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before. 
    tfile = open("/sys/bus/w1/devices/10-000802824e58/w1_slave") 
    # Read all of the text in the file. 
    text = tfile.read() 
    # Close the file now that the text has been read. 
    tfile.close() 
    # Split the text with new lines (\n) and select the second line. 
    secondline = text.split("\n")[1] 
    # Split the line into words, referring to the spaces, and select the 10th word (counting from 0). 
    temperaturedata = secondline.split(" ")[9] 
    # The first two characters are "t=", so get rid of those and convert the temperature from a string to a number. 
    temperature = float(temperaturedata[2:]) 
    # Put the decimal point in the right place and display it. 
    temperature = temperature/1000 
    print temperature 
    time.sleep(5) 
+0

这将抛出'SyntaxError' –

+0

@ juanpa.arrivillaga什么是语法错误?刚刚离开电脑。 –

+2

'虽然真的:'是无效的Python –

0
  1. 在Python中,你需要indent你的代码,(一般采用2个或4个空格)。
  2. while python中的循环以小写w开头。
  3. 将您的导入语句放在文件的顶部。

像这样:

import time 

while True: 
    # Open the file that we viewed earlier so that python can see what is in it. Replace the serial number as before. 
    tfile = open("/sys/bus/w1/devices/10-000802824e58/w1_slave") 
    # Read all of the text in the file. 
    text = tfile.read() 
    # Close the file now that the text has been read. 
    tfile.close() 
    # Split the text with new lines (\n) and select the second line. 
    secondline = text.split("\n")[1] 
    # Split the line into words, referring to the spaces, and select the 10th word (counting from 0). 
    temperaturedata = secondline.split(" ")[9] 
    # The first two characters are "t=", so get rid of those and convert the temperature from a string to a number. 
    temperature = float(temperaturedata[2:]) 
    # Put the decimal point in the right place and display it. 
    temperature = temperature/1000 
    print temperature 
    time.sleep(5)