Reputation: 2744
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)
Upvotes: 0
Views: 3985
Reputation: 42000
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.
Upvotes: 1
Reputation: 24282
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
Upvotes: 1
Reputation: 512
If you get
ImportError: No module named RPi.GPIO
you need to install the module first
https://pypi.python.org/pypi/RPi.GPIO
Upvotes: 0