0

I'm new in python and total stranger to python indentation. I get syntax error whenever I try to run the following code, what is the problem with it? Thanks already.

#!/usr/bin/python

import RPi.GPIO as GPIO
import time

def RC_Analog (Pin):  
 counter = 0  
 # Discharge capacitor  
 GPIO.setup(Pin, GPIO.OUT)  
 GPIO.output(Pin, GPIO.LOW)  
 time.sleep(0.1)  
 GPIO.setup(Pin, GPIO.IN)  
 # Count loops until voltage across capacitor reads high on GPIO  
 while(GPIO.input(Pin)==GPIO.LOW):  
  counter =counter+1  
 return counter  

# Set up header pin 11 as an input
triggerPin = 25;
echoPin = 8;
GPIO.setmode(GPIO.BCM)
GPIO.setup(triggerPin, GPIO.OUT)
GPIO.setup(echoPin, GPIO.IN)

while True:
 GPIO.output(triggerPin, False)
 time.sleep(0.000002)
 GPIO.output(triggerPin, True)
 time.sleep(0.00001)
 GPIO.output(triggerPin, False)
 print RC_Analog(echoPin)/58
 time.sleep (0.25)
13
  • 1
    Can you show us the actual error message? Commented Apr 20, 2013 at 13:59
  • The SyntaxError will tell you which line the problem is on. Commented Apr 20, 2013 at 13:59
  • def?RC_Analog(Pin) it highlights the question marked area Commented Apr 20, 2013 at 14:01
  • 1
    Can you provide the full stack trace? Commented Apr 20, 2013 at 14:02
  • 4
    If it literally prints def?RC_Analog(Pin), it sounds like you have something other than a space between the def and the RC_Analog, maybe some weird Unicode character which is similar to a space. Commented Apr 20, 2013 at 14:05

3 Answers 3

1

Are you really using python to launch your script ? I can get an error at the same place if I try to source it as a shell script:

. test.py

Make sure you launch it with python:

python test.py

or make it executable: chmod u+x test.py and launch it with: ./test.py

Sign up to request clarification or add additional context in comments.

1 Comment

your answer is useful, but nothing i didnt already do. thanks anyway. The problem was with the utf-8 whitespace thing, it turned out.
1

def?RC_Analog(Pin) it highlights the question marked area

If you see the code as def RC_Analog(Pin), but the syntax error message literally prints def?RC_Analog(Pin), it sounds like you have something other than an ASCII space character between the def and the RC_Analog, like a Unicode non-breaking space, or some other Unicode character which is similar to a space.

Replacing it with a space typed from your keyboard should solve the problem.

When writing a Python script, it's best to ensure you only use the 7-bit ASCII character set. Some text editors will let you set this in a configuration option, others will let you choose an encoding when saving.

If you're using Windows Notepad, select type "ANSI" when saving.

Comments

0

If you get

ImportError: No module named RPi.GPIO

you need to install the module first

https://pypi.python.org/pypi/RPi.GPIO

3 Comments

ImportError is different from SyntaxError.
Well in John Brown's defense, you are being pretty vague about exactly what error you're getting.
I know but as i said, im new to it, so i dont know what information else should i provide, the only information shell gives to me is that there is a syntax error.

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.