0

I'm an absolute beginner when it comes to python and raspberry pi, I'm trying to just print a button pressed log when I press the button, however, I keep getting many errors. and after hours of struggling, i'm at the last one which says and excuse me for such a noob question but it says invalid syntax. now have googled python syntax and according to me what I have atm is correct. could someone help me out and explain what is happening? Thanks

The error is :

File "test.py", line 1 syntax Error : invalid syntax

test.py

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
input_state = GPIO.input(18)
if input_state == False:
    print('Button Pressed')
    time.sleep(0.2)
4
  • Please include the Stack trace or the errors you're receiving! Commented Aug 10, 2017 at 12:44
  • Did you check you have RPi.GPIO in you environment? Commented Aug 10, 2017 at 12:57
  • How do i know that? Commented Aug 10, 2017 at 12:59
  • i know that GPIO comes with the rasberry pi Commented Aug 10, 2017 at 13:00

1 Answer 1

4

Check your indentation:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    input_state = GPIO.input(18)
    if input_state == False:
        print('Button Pressed')
        time.sleep(0.2)
Sign up to request clarification or add additional context in comments.

10 Comments

at which line exactly?
after while True:
this is just the copy paste, but i have it exactly as you posted in ur answer
When i run it just says "invalid syntax"
File "test.py", line 1 syntax Error : invalid syntax
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.