etek
Posts: 3
Joined: Wed Sep 04, 2013 11:48 am

syntax error in getting input

Wed Sep 04, 2013 12:16 pm

my very first program

Code: Select all

# -*- coding: utf-8 -*-                                     
import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
gpio.setup(17, gpio.IN)

while True:
    input_value = gpio.input(17)
    if input_value == False:
        print('The button has been pressed...')
        while input_value == False:
            input_value = gpio.input(17)
Error
First line is automatically included when i run my prog
and my syntax error is

File "button.py" , line 8
input_value = gpio.input(17)
^
SyntaxError : invalid syntax

//help me :|

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: syntax error in getting input

Wed Sep 04, 2013 2:45 pm

Check the layout of the program script - indentation is critical with python.

Look very carefully at what happens after "while True:" and before " input_value = gpio.input(17)"
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

etek
Posts: 3
Joined: Wed Sep 04, 2013 11:48 am

Re: syntax error in getting input

Thu Sep 05, 2013 1:10 pm

Thanks! the problem is with indentation, but still i could not get the desired output.
DeeJay wrote:Check the layout of the program script - indentation is critical with python.

Look very carefully at what happens after "while True:" and before " input_value = gpio.input(17)"

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: syntax error in getting input

Thu Sep 05, 2013 4:13 pm

My experience with your script -

Code: Select all

[email protected] ~ $ cat etek.py
# -*- coding: utf-8 -*-
import RPi.GPIO as gpio
gpio.setmode(gpio.BCM)
gpio.setup(17, gpio.IN)

while True:
    input_value = gpio.input(17)
    if input_value == False:
        print('The button has been pressed...')
        while input_value == False:
            input_value = gpio.input(17)

When run, gives -

Code: Select all

[email protected] ~ $ sudo python etek.py
The button has been pressed...


(I have nothing connected to the gpio header on the RPi.)
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

etek
Posts: 3
Joined: Wed Sep 04, 2013 11:48 am

Re: syntax error in getting input

Wed Sep 11, 2013 9:45 am

Hey there I got my output after I changed my 10k resistor to 1k.

vijay_nav
Posts: 6
Joined: Thu Oct 17, 2013 11:08 am

Re: syntax error in getting input

Sat Oct 19, 2013 4:07 am

i am facing an error in 17 line this syntax error is
if GPIO.input(8)
please help me
# Import the required modules.
import RPi.GPIO as GPIO
import time
# Set the numbering sequence of the pins, then set pins ten and twelve to output, and pin eight to input.
GPIO.setmode(GPIO.BOARD)
GPIO.setup(10, GPIO.OUT)`
GPIO.setup(12, GPIO.OUT)
GPIO.setup(8, GPIO.IN)
# Turn both of the LEDs off.
GPIO.output(10, True)
GPIO.output(12, True)
# The SwitchState variable is 1 if the button is pressed, and 0 otherwise. LEDState is 0 when off, 1 when red, and 2 when green.
SwitchState = 0
LEDState = 0

while 1:
if GPIO.input(8)
# When the LED is off, keep the green LED off, turn the red one on, then change the state of the LED to reflect that it is red, and wait one second.
if LEDState == 0
GPIO.output(10, True)
GPIO.output(12, False)
LEDState = 1
time.sleep(1)

# When the LED is red, turn the green LED on, turn the red one off, then change the state of the LED to reflect that it is green, and wait one second.
elif LEDState == 1
GPIO.output(10, False)
GPIO.output(12, True)
LEDState = 2
time.sleep(1)

# When the LED is green, turn it off, then turn the red one off, then change the state of the LED to reflect that they are all off, and wait one second.
elif LEDState == 2
GPIO.output(10, True)
GPIO.output(12, True)
LEDState = 0
time.sleep(1)

User avatar
DeeJay
Posts: 2027
Joined: Tue Jan 01, 2013 9:33 pm
Location: East Midlands, UK

Re: syntax error in getting input

Sat Oct 19, 2013 8:41 am

vijay_nav wrote:i am facing an error in 17 line this syntax error is
if GPIO.input(8)
please help me
That line is incomplete.

Have a look at some Python documentation -

http://www.tutorialspoint.com/python/python_if_else.htm

http://docs.python.org/2/tutorial/controlflow.html


[Since Python relies on indentation as part of its syntax, please post sample code within a CODE block by using the [

Code: Select all

][/b] box at the top of the message editor screen.]
How To Ask Questions The Smart Way: http://www.catb.org/~esr/faqs/smart-questions.html
How to Report Bugs Effectively: http://www.chiark.greenend.org.uk/~sgtatham/bugs.html

Return to “Beginners”