Killerspec
Posts: 31
Joined: Thu Sep 06, 2012 10:41 pm

IndentationError

Wed Feb 20, 2013 1:21 am

Code: Select all

#!/usr/bin/python

import time
import RPi.GPIO as GPIO
GPIO.setup(11, GPIO.IN)

while True:
	mybutton = GPIO.input(11)
	     if mybutton == False:
             print "giggle"
	     time.sleep(.2)
If I run this code I get an indentationError underlining the 'i' in if mybutton.

Please help

cae2100
Posts: 143
Joined: Tue Jan 08, 2013 10:35 am
Contact: Website

Re: IndentationError

Wed Feb 20, 2013 3:12 am

the problem is the indentation, just use 4 spaces instead of tab key unless you have your editor set up for 4 spaces. Mybutton routine only has 3 spaces in front of it, here's the fixed example.

Code: Select all

#!/usr/bin/python

import time
import RPi.GPIO as GPIO
GPIO.setup(11, GPIO.IN)

while True:
    mybutton = GPIO.input(11)
        if mybutton == False:
            print "giggle"
        time.sleep(.2)
If it isnt smoking and blown into pieces, it's not pushed far enough yet. :P
http://cae2100.wordpress.com

User avatar
croston
Posts: 716
Joined: Sat Nov 26, 2011 12:33 pm
Location: Blackpool
Contact: Website

Re: IndentationError

Wed Feb 20, 2013 7:08 am

It doesn't matter how many spaces you have used as long as it is consistent within the same block. Four spaces is what most people use. The big thing to watch out for is using tabs for indentation. My advice on this is don't use tabs for indentation. At all. Ever.

Killerspec
Posts: 31
Joined: Thu Sep 06, 2012 10:41 pm

Re: IndentationError

Wed Feb 20, 2013 12:14 pm

I rewrote that small program again using sudo nano I first used the TAB button for the indent and then used space bar for the second program but still coming up with same indent error.

When I downloaded the all the kit the used RPi.GPIO 0.4.2a download as it was the lastest version would this make a difference also I updated python-dev on the Pi.

Thanks a miilion for the help I have been going around in circles for a week now checking forums and searchin the hell out the web to make sure I aint an idiot.

Matthew

KCarscadden
Posts: 57
Joined: Sat Jun 09, 2012 9:00 pm
Location: Canada

Re: IndentationError

Wed Feb 20, 2013 3:32 pm

The example should be:

Code: Select all

#!/usr/bin/python

import time
import RPi.GPIO as GPIO
GPIO.setup(11, GPIO.IN)

while True:
    mybutton = GPIO.input(11)
    if mybutton == False:
        print "giggle"
    time.sleep(.2)
The number of spaces before the 6th, 7th and 9th statements doesn't matter, provided the number of spaces is all the same.
The number of spaces before the 8th statement doesn't matter, provided the number of spaces is more than the number before the 6th, 7th and 9th.

Killerspec
Posts: 31
Joined: Thu Sep 06, 2012 10:41 pm

Re: IndentationError

Wed Feb 20, 2013 8:37 pm

Thanks for the help now I am coming up with a whole new exception.

Message:

Traceback (most recent call last):
File "mybutton1.py", line 5, in <module>
GPIO.setup(11, GPIO.IN)
RPi.GPIO.ModeNotSetException: Please set mode using GPIO.setmode (GPIO.BOARD) or GPIO.setmode(GPIO.BCM)

Yet again thanks for all the help.

Matt

Killerspec
Posts: 31
Joined: Thu Sep 06, 2012 10:41 pm

Re: IndentationError

Wed Feb 20, 2013 8:47 pm

Ok I found out what was going on I was missing a line of code!

Return to “Python”