2013-08-09 4 views
0

안녕 난이 코드 - 내 머리 라운드 indentaion 수준을 얻기 위해 struging하고파이썬 들여 쓰기 초보자

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

해당/다른 사람이 동안해야하며 자신의 코드가 휴식 루프의 외부 이해가되지 않습니다, 들여 쓰기를 할 필요가있는 경우

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) 
+0

감사합니다. 이 코드는 내가 들었을하고 내 자신의 사용을 위해 내가 초보자로 수정 :) 아이디어는 그 기능이 10 번 내가 믿는 가치를 검색하려고합니다. –

+0

Break는 루프 구조에서 벗어나 반복을 중지하므로 루프에 중첩되어야합니다. 여기서 우리는 잠시 동안 휴식을 볼 수 있습니다. 그래서 휴식은 잠시 동안을 의미합니다. 루프가 하나 더 있으면 내부 루프에서 빠져 나옵니다. 반복문을 사용하는 것은 불가능합니다. 아무런 반복이 없기 때문에 아무 의미가 없습니다. 시도해보십시오. 오류가 발생합니다. – lc2817

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

조건뿐만 아니라 들여 쓰기를해야하는 경우 - 다른. 당신이 가지고있는 것처럼

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을 전달했습니다.

+0

즉 기본 모드로 읽기 전용 인수가 필요하지 않습니다. –

+0

감사합니다. 나는 당신이 제안한 변경을했습니다. 파일을 열고, 읽고 닫는 코드의 두 번째 줄이 들여 쓰기되어야한다고 생각합니까? –

+0

예, 들여 쓰기를해야합니다. – xbonez

0

그동안 블록 indentaion은 다음과 같이해야합니다 :

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 
-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 개가 있어야하는데 왜 공백을 넣었습니까? 해봐! :)

EDIT : if 문은 if : 아래의 내용은 4 칸 들여 쓰기해야합니다! elif와 다른 것! :)

+0

1 공백은 공자가 사용하는 것입니다! 심각하게도, 파이썬은 여러분과 여러분의 작업 환경이 좋아하는 것처럼 어느 정도의 여백을 가질 수 있습니다. 2? 2를 사용하고 8을 원하십니까? 8. 그걸로 아무것도 잘못 사용하지 마십시오. –

0

의 들여 쓰기 문제가 있습니다 if 문 :

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