Noob查询Python(Raspberry Pi)

时间:2016-12-31 21:14:02

标签: python raspberry-pi

我能够成功运行代码,但希望永远循环它。这是我在Python的第一次尝试,任何帮助将不胜感激。我所做的是在代码之上放置了一个“While True:”,但是我一直收到“抱歉缩进错误:预期缩进的第3行”的错误,但没有它,它工作正常但只运行一次。所以我的问题是如何在不收到错误的情况下永远循环?为什么我不能写“真实:”作为第一行?

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)

2 个答案:

答案 0 :(得分:1)

您需要在while True:语句下面的所有代码中添加四个空格。  Python在块中工作,while语句启动一个块,这意味着块之后的所有代码都有4个空格。此外,import语句应位于顶部,因为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)

答案 1 :(得分:0)

  1. 在python中,您需要indent您的代码,(通常使用2或4个空格)。
  2. 虽然python中的循环以小写w开头。
  3. 将导入语句放在文件的顶部。
  4. 像这样:

    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)