Casabian
Posts: 6
Joined: Mon Sep 10, 2012 12:23 pm

IndentationError

Wed Apr 24, 2013 1:16 pm

Hi,

I'm new to python (and programming), and am trying to set up my raspberry pi with a DS18B20 temperature sensor.

I'm using adafruit's guide to set up the sensor, but I'm getting an error on the code.
http://learn.adafruit.com/adafruits-ras ... re-sensing
Here's the code I'm using:

Code: Select all

import os
import glob
import time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c, temp_f
while True:
print(read_temp())
time.sleep(1)
The error I'm getting is

Code: Select all

IndentationError: expected an indented block" on line 10 
f = open(device_file, 'r')
I can pull temperature values from the sensor in terminal with the cat command.

dr_d_gee
Posts: 84
Joined: Fri Jan 04, 2013 1:30 pm

Re: IndentationError

Wed Apr 24, 2013 1:33 pm

In Python, the structure of code is determined by indentation (there are NO begins, ends or curly brackets). So the code immediately after the line with the 'def' statement needs to be indented.

If you use a Python-aware editor like Idle it will indent the code for you in such circumstances--but you will still need to know where the indentation needs to end.
Last edited by dr_d_gee on Thu Apr 25, 2013 12:24 pm, edited 1 time in total.

Casabian
Posts: 6
Joined: Mon Sep 10, 2012 12:23 pm

Re: IndentationError

Wed Apr 24, 2013 2:24 pm

You're right. I compared to the original code, the indentations hadn't copied over.

Thanks for the heads up!

Return to “Python”