2016-12-31 4 views
-3

コードを正常に実行できましたが、これを永遠にループすることを望んでいました。 Pythonでの私の最初の試みは、どんな助けにも感謝します。私がしたことは、コードの上に "While True:"が置かれていますが、 "Sorry Indentation Error:インデントされたブロック行3が予想されます"というエラーが表示されますが、それがなければ、だから私の質問はエラーを取得せずに永遠にループすることができますか?最初の行として "While True:"と書くことができないのはなぜですか?Python(Raspberry Pi)に関するNoobの問い合わせ

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:ステートメントの下にあるすべてのコードに4つのスペースを追加する必要があります。 Pythonはブロックで動作しますが、while文はブロックを開始します。つまり、ブロック内のすべてのコードの後ろに4つのスペースがあります。 Pythonはコードを知るために複数回ではなく、一度だけコードをインポートすればよいので、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) 
+0

これは 'SyntaxError'をスローします –

+0

@ juanpa.arrivillaga構文エラーとは何ですか?ちょうどコンピュータから歩いていた。 –

+2

'True:'は無効ですPython –

0
  1. あなたは、あなたのコードをindentする必要があります(一般的に2つのまたは4つのスペースを使用します)。
  2. while Pythonのwhileループは、小文字の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)