Page 1 of 1

IndentationError

Posted: Wed Feb 20, 2013 1:21 am
by Killerspec

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

Re: IndentationError

Posted: Wed Feb 20, 2013 3:12 am
by cae2100
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)

Re: IndentationError

Posted: Wed Feb 20, 2013 7:08 am
by croston
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.

Re: IndentationError

Posted: Wed Feb 20, 2013 12:14 pm
by Killerspec
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

Re: IndentationError

Posted: Wed Feb 20, 2013 3:32 pm
by KCarscadden
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.

Re: IndentationError

Posted: Wed Feb 20, 2013 8:37 pm
by Killerspec
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

Re: IndentationError

Posted: Wed Feb 20, 2013 8:47 pm
by Killerspec
Ok I found out what was going on I was missing a line of code!