我最近得到了一个树莓派,并一直在尝试用它运行 python 代码。但是,每当我尝试在终端中执行此操作时,都会弹出以下错误:
File "Python-3.6.5/rc_car.py", line 1 Python 3.5.3 (default, Jan 19 2017, 14:11:04) ^
SyntaxError: invalid syntax
我尝试运行的文件名为rc_car.py
。我已经将我的树莓派更新到了 python v3.6,所以我不认为我的系统已经过时了。另外,我确保我的内存中只有一个名为 rc_car.py
的文件,所以我认为也不是它。
有谁知道为什么会发生这种情况?
如果有帮助的话,这是代码(我包括了 shell,因为这似乎是错误所在):
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "copyright", "credits" or "license()" for more information.
>>> import RPi.GPIO as io
io.setmode(io.BCM)
import sys, tty, termios, time
# These two blocks of code configure the PWM settings for
# the two DC motors on the RC car. It defines the two GPIO
# pins used for the input, starts the PWM and sets the
# motors' speed to 0
motor1_in1_pin = 4
motor1_in2_pin = 17
io.setup(motor1_in1_pin, io.OUT)
io.setup(motor1_in2_pin, io.OUT)
motor1 = io.PWM(4,100)
motor1.start(0)
motor1.ChangeDutyCycle(0)
motor2_in1_pin = 24
motor2_in2_pin = 25
io.setup(motor2_in1_pin, io.OUT)
io.setup(motor2_in2_pin, io.OUT)
motor2 = io.PWM(4,100)
motor2.start(0)
motor2.ChangeDutyCycle(0)
# Defining the GPIO pins that will be used for the LEDs on
# the RC car and setting the output to false
io.setup(18, io.OUT)
io.output(18, False)
io.setup(23, io.OUT)
io.output(23, False)
# The getch method can determine which key has been pressed
# by the user on the keyboard by accessing the system files
# It will then return the pressed key as a variable
def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
# This section of code defines the methods used to determine
# whether a motor needs to spin forward or backwards. The
# different directions are acheived by setting one of the
# GPIO pins to true and the other to false. If the status of
# both pins match, the motor will not turn.
def motor1_forward():
io.output(motor1_in1_pin, True)
io.output(motor1_in2_pin, False)
def motor1_reverse():
io.output(motor1_in1_pin, False)
io.output(motor1_in2_pin, True)
def motor2_forward():
io.output(motor2_in1_pin, True)
io.output(motor2_in2_pin, False)
def motor2_reverse():
io.output(motor2_in1_pin, False)
io.output(motor2_in2_pin, True)
# This method will toggle the lights on/off when the user
# presses a particular key. It will then change the status
# of the lights so it will know whether to turn them on or
# off when it is next called.
def toggleLights():
global lightStatus
if(lightStatus == False):
io.output(18, True)
io.output(23, True)
lightStatus = True
else:
io.output(18, False)
io.output(23, False)
lightStatus = False
# This method will toggle the direction of the steering
# motor. The method will determine whether the user wants
# to turn left or right depending on the key they press and
# then make the appropriate adjustment. It works as a toggle
# because the program cannot read multiple pressed keys at
# the same time. The possible positions of the wheels are
# "right", "centre" and "left". It will then update the
# status of the wheel to access next time it is called.
def toggleSteering(direction):
global wheelStatus
if(direction == "right"):
if(wheelStatus == "centre"):
motor1_forward()
motor1.ChangeDutyCycle(99)
wheelStatus = "right"
elif(wheelStatus == "left"):
motor1.ChangeDutyCycle(0)
wheelStatus = "centre"
if(direction == "left"):
if(wheelStatus == "centre"):
motor1_reverse()
motor1.ChangeDutyCycle(99)
wheelStatus = "left"
elif(wheelStatus == "right"):
motor1.ChangeDutyCycle(0)
wheelStatus = "centre"
# Setting the PWM pins to false so the motors will not move
# until the user presses the first key
io.output(motor1_in1_pin, False)
io.output(motor1_in2_pin, False)
io.output(motor2_in1_pin, False)
io.output(motor2_in2_pin, False)
# Global variables for the status of the lights and steering
lightStatus = False
wheelStatus = "centre"
# Instructions for when the user has an interface
print("w/s: acceleration")
print("a/d: steering")
print("l: lights")
print("x: exit")
# Infinite loop that will not end until the user presses the
# exit key
while True:
# Keyboard character retrieval method is called and saved
# into variable
char = getch()
# The car will drive forward when the "w" key is pressed
if(char == "w"):
motor2_forward()
motor2.ChangeDutyCycle(99)
# The car will reverse when the "s" key is pressed
if(char == "s"):
motor2_reverse()
motor2.ChangeDutyCycle(99)
# The "a" key will toggle the steering left
if(char == "a"):
toggleSteering("left")
# The "d" key will toggle the steering right
if(char == "d"):
toggleSteering("right")
# The "l" key will toggle the LEDs on/off
if(char == "l"):
toggleLights()
# The "x" key will break the loop and exit the program
if(char == "x"):
print("Program Ended")
break
# At the end of each loop the acceleration motor will stop
# and wait for its next command
motor2.ChangeDutyCycle(0)
# The keyboard character variable will be set to blank, ready
# to save the next key that is pressed
char = ""
# Program will cease all GPIO activity before terminating
io.cleanup()
[DEBUG ON]
>>>
[DEBUG OFF]
>>>
最佳答案
您似乎将整个交互式 session 复制粘贴为 Python 脚本。您必须删除文件开头和结尾的部分:
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "copyright", "credits" or "license()" for more information.
>>>
[DEBUG ON]
>>>
[DEBUG OFF]
>>>
这些部分不是有效的 Python 代码,因为它们只是来自交互式 Python shell 的提示。如果您从博客或网站复制此内容,他们并没有明确表明它不是“按原样”的 Python 文件,而是交互式 Python shell 的转录本。
关于python - Raspberry pi 终端不断提示存在语法错误,但仅指向 shell 顶部的 "Python 3.5.3",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59479954/