indentation error

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 565 Views - Last Post: 18 August 2014 - 02:42 PM Rate Topic: -----

#1 sachin.sunny96  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 14

indentation error

Posted 13 August 2014 - 07:50 AM

# Import the Raspberry pi GPIO module.
import RPi.GPIO as GPIO
GPIO.setwarnings(False) # supress the system warnings
GPIO.cleanup() # clean up the GPIO pins i.e make the GPIO pins to low
# Set the mode of numbering the pins.
GPIO.setmode(GPIO.BOARD)
#function to call festival TTS system
def speak(text):
from subprocess import PIPE, Popen                                            
process = Popen(['festival', '--tts'], stdin=PIPE)
process.stdin.write(text + '\n')
process.stdin.close()
process.wait()
GPIO.setup(11, GPIO.IN)
#GPIO pin 11 is the input.
GPIO.setup(12, GPIO.IN)
#GPIO pin 12 as input
GPIO.setup(13, GPIO.IN)
#GPIO pin 13 as input pin
left = GPIO.input(11)
front = GPIO.input(12)
right = GPIO.input(13)
#keep on polling for input pins
while 1:
left = GPIO.input(11)
front = GPIO.input(12)
right = GPIO.input(13)
if left == True or front == True or right == True:
if left == False and front == False and right == True:
speak("move either left ot front")
else:
speak("you can move in any direction")


--------------------------------------------------------------------------------------
when i type
$ sudo python filename.py
it is showing "line 9 indentationerror : expected an indented block"
please help me how to resolve this error

This post has been edited by andrewsw: 13 August 2014 - 07:58 AM
Reason for edit:: Added CODE tags


Is This A Good Question/Topic? 0
  • +

Replies To: indentation error

#2 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,790
  • Joined: 12-December 12

Re: indentation error

Posted 13 August 2014 - 08:02 AM

We can't see any indents at all as you haven't used CODE tags. it demonstrates how to use them directly in the area where you are typing your post. They are essential for Python code.

I've just added the code tags but there still isn't any indenting. You'll need to re-post your code.

Common Python errors

Quote

Indentation of program statements is critical to the readability of code. Most programming languages permit indentation, but don't enforce it. Python enforces it with an iron fist. Whenever you have a situation with code inside of a statement (such as the code that defines the main() function), that 'inside' code must be indented, and must be indented consistently.

Was This Post Helpful? 1
  • +
  • -

#3 sachin.sunny96  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 14

Re: indentation error

Posted 13 August 2014 - 08:04 AM

thank you very much for replying.
I am posting my full code here. It is for guiding visually challenged persons using raspberry pi.
------------------------------------------------------
# Import the Raspberry pi GPIO module.
import RPi.GPIO as GPIO
GPIO.setwarnings(False) # supress the system warnings
GPIO.cleanup() # clean up the GPIO pins i.e make the GPIO pins to low
# Set the mode of numbering the pins.
GPIO.setmode(GPIO.BOARD)
#function to call festival TTS system
def speak(text):
from subprocess import PIPE, Popen
process = Popen(['festival', '--tts'], stdin=PIPE)
process.stdin.write(text + '\n')
process.stdin.close()
process.wait()
GPIO.setup(11, GPIO.IN)
#GPIO pin 11 is the input.
GPIO.setup(12, GPIO.IN)
#GPIO pin 12 as input
GPIO.setup(13, GPIO.IN)
#GPIO pin 13 as input pin
left = GPIO.input(11)
front = GPIO.input(12)
right = GPIO.input(13)
#keep on polling for input pins
while 1:
left = GPIO.input(11)
front = GPIO.input(12)
right = GPIO.input(13)
if left == True or front == True or right == True:
if left == False and front == False and right == True:
speak("move either left ot front")
elif left == False and front == True and right == False:
speak("move either left or right")
elif left == False and front == True and right == True:
speak("move left")
elif left == True and front == False and right == False:
speak("move either front or right")
elif left == True and front == False and right == True:
speak("move front")
elif left == True and front == True and right == False:
speak("move right")
elif left == True and front == True and right == True:
speak("better to go back")
else:
speak("you can move in any direction")
---------------------------------------------------
Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,790
  • Joined: 12-December 12

Re: indentation error

Posted 13 August 2014 - 08:11 AM

That is still not using code tags, and still not showing your indenting.

How to use code tags
Was This Post Helpful? 1
  • +
  • -

#5 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 673
  • View blog
  • Posts: 2,958
  • Joined: 03-December 12

Re: indentation error

Posted 13 August 2014 - 08:12 AM

The error is not hard to follow. Look at your code in the IDE and move the line into the correct block of code.
Was This Post Helpful? 1
  • +
  • -

#6 sachin.sunny96  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 14

Re: indentation error

Posted 13 August 2014 - 08:17 AM

I am from electronics background and I really don't know python language. I don't understand what is "code tags". Its my complete code copied from notepad.
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,790
  • Joined: 12-December 12

Re: indentation error

Posted 13 August 2014 - 08:22 AM

If you have a function like this,
def someFunction():

then all the lines that belong to the function need to be indented consistently below it,
def someFunction():
    # some code
    # more code at the same indent
    pass

# other code that is not part of the function 
# is not indented

You can use a tab, four (or two) spaces for the indents. Whichever you decide to use you should use consistently throughout.

My code is within code-tags, so it appears with line numbers and retains the original indenting. My screencast (did you view it?) shows you how to add these tags.

This post has been edited by andrewsw: 13 August 2014 - 08:25 AM

Was This Post Helpful? 1
  • +
  • -

#8 sachin.sunny96  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 14

Re: indentation error

Posted 13 August 2014 - 08:26 AM

thank you. now I get it. I will try to make code correct as you have said.
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,790
  • Joined: 12-December 12

Re: indentation error

Posted 13 August 2014 - 08:27 AM

Good ;)

Note that this is not just for functions; code for if, elif and else clauses also needs to be indented. Basically, anything after a line that ends with a colon:

This post has been edited by andrewsw: 13 August 2014 - 08:29 AM

Was This Post Helpful? 0
  • +
  • -

#10 sachin.sunny96  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 14

Re: indentation error

Posted 13 August 2014 - 08:36 AM

# Import the Raspberry pi GPIO module.
import RPi.GPIO as GPIO
GPIO.setwarnings(False) # supress the system warnings
GPIO.cleanup() # clean up the GPIO pins i.e make the GPIO pins to low
# Set the mode of numbering the pins.
GPIO.setmode(GPIO.BOARD)
#function to call festival TTS system
def speak(text):
from subprocess import PIPE, Popen
process = Popen(['festival', '--tts'], stdin=PIPE)
process.stdin.write(text + '\n')
process.stdin.close()
process.wait()
GPIO.setup(11, GPIO.IN)
#GPIO pin 11 is the input.
GPIO.setup(12, GPIO.IN)
#GPIO pin 12 as input
GPIO.setup(13, GPIO.IN)
#GPIO pin 13 as input pin
left = GPIO.input(11)
front = GPIO.input(12)
right = GPIO.input(13)
#keep on polling for input pins
while 1:
left = GPIO.input(11)
front = GPIO.input(12)
right = GPIO.input(13)
if left == True or front == True or right == True:
if left == False and front == False and right == True:
speak("move either left ot front")
elif left == False and front == True and right == False:
speak("move either left or right")
elif left == False and front == True and right == True:
speak("move left")
elif left == True and front == False and right == False:
speak("move either front or right")
elif left == True and front == False and right == True:
speak("move front")
elif left == True and front == True and right == False:
speak("move right")
elif left == True and front == True and right == True:
speak("better to go back")
else:
speak("you can move in any direction")

-----------------------------------------
now is it correct?

I attached my file here. if you have time please see it.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#11 fryishone  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 53
  • Joined: 09-May 14

Re: indentation error

Posted 13 August 2014 - 08:38 AM

Does it run?
Was This Post Helpful? 1
  • +
  • -

#12 sachin.sunny96  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 14

Re: indentation error

Posted 13 August 2014 - 08:40 AM

My code has two if statements. which one is correct?
-----------------------------------------------------------
if left == True or front == True or right == True:
if left == False and front == False and right == True:
speak("move either left ot front")
-----------------------------------------------------------------
if left == True or front == True or right == True:
if left == False and front == False and right == True:
speak("move either left ot front")
Was This Post Helpful? 0
  • +
  • -

#13 fryishone  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 53
  • Joined: 09-May 14

Re: indentation error

Posted 13 August 2014 - 08:42 AM

Without indentation they are identical.

if left == True or front == True or right == True:
  if left == False and front == False and right == True:
    speak("move either left ot front")



Would be the correct indentation though.
Was This Post Helpful? 1
  • +
  • -

#14 sachin.sunny96  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 13-August 14

Re: indentation error

Posted 13 August 2014 - 08:42 AM

how to post reply without changing code indentation?

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#15 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,790
  • Joined: 12-December 12

Re: indentation error

Posted 13 August 2014 - 08:44 AM

Those two code fragments (in the previous post) are identical, because you still haven't made an attempt to wrap the code in code-tags.

Quote

how to post reply without changing code indentation?

I have already provided a screencast, and mentioned that it demonstrates how to use the tags in the area where you are typing your post.

This post has been edited by andrewsw: 13 August 2014 - 08:47 AM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2