gpt4 book ai didi

Python 返回无效语法错误

转载 作者:行者123 更新时间:2023-11-30 23:13:41 25 4
gpt4 key购买 nike

我正在树莓派上编写一个简单的Python程序,我对Python编程很陌生。我定义了一个名为 GetMessage 的函数,它没有参数并返回一个我称为 data 的变量,但我收到一个错误,指出

File "Raspberry_pi.py", line 39

return none

    ^

SyntaxError: invalid syntax

import os
import glob
import time
import RPi.GPIO as GPIO
from math import *
from bluetooth import *
from RPIO import PWM

os.system('sudo hciconfig hci0 pisca')
os.system('sudo hciconfig hci0 name "De Quadcoptur"')
servo = PWM.Servo()
StartSpin()

server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)

port = server_sock.getsockname()[1]

GetMessage()
DecodeInput()

uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
def GetMessage():
advertise_service( server_sock, "XT1032", #phone bluetooth name
service_id = uuid,
service_classes = [ uuid, SERIAL_PORT_CLASS ],
profiles = [ SERIAL_PORT_PROFILE ],
#protocols = [ OBEX_UUID ]
)
client_sock, client_info = server_sock.accept()

try:
data = client_sock.recv(1024)
if len(data) == 0: break
print "received [%s]" % data
client_sock.close()
server_sock.close()
except IOError:
pass

break
return data


def StartSpin():
# Set servo on GPIO17 to 1200µs (1.2ms)
servo.set_servo(17, 1000)
servo.set_servo(18, 1000)
servo.set_servo(19, 1000)
servo.set_servo(20, 1000)
time.sleep(1)
servo.stop_servo(17)
servo.stop_servo(18)
servo.stop_servo(19)
servo.stop_servo(20)
#Check if more pulses is faster
time.sleep(2000)
PWM.add_channel_pulse(0, 17, start = 1000, width = 100)
PWM.add_channel_pulse(0, 17, start = 1000, width = 100)
PWM.add_channel_pulse(0, 17, start = 1000, width = 100)
PWM.add_channel_pulse(0, 17, start = 1000, width = 100)
PWM.add_channel_pulse(0, 17, start = 1000, width = 100)
servo.stop_servo(17)
servo.stop_servo(18)
servo.stop_servo(19)
servo.stop_servo(20)
return None

def DecodeInput():
data = GetMessage()
if(data == 'start')
StartSpin()
return 0
else if(data[0] == 'U')
data.strip('U')
UpPower = int(data)
SetUpPower(UpPower)
else if(data[0] == 'P')
data.strip('P')
PitchPower = int(data)
SetPitchPower
else
data.strip('P')
RollPower = int(data)
SetPower(UpPower, PitchPower, RollPower)
return None

def SetPower(UpPower, PitchPower, RollPower):
#Make Arbitrary Values
Motor1Power = UpPower #Front Left
Motor2Power = UpPower #Front Right
Motor3Power = UpPower #Back Left
Motor4Power = UpPower #Back Right
PitchPower = PitchPower /2
RollPower = RollPower /2
if(PitchPower < 25)
Motor1Power = Motor1Power + abs(25-PitchPower)
Motor2Power = Motor1Power + abs(25-PitchPower)
else
Motor3Power = Motor3Power + (PitchPower-25)
Motor4Power = Motor4Power + (PitchPower-25)
if(RollPower < 25)
Motor1Power = Motor1Power + abs(25-RollPower)
Motor3Power = Motor3Power + abs(25-RollPower)
else
Motor2Power = Motor2Power + (RollPower - 25)
Motor4Power = Motor4Power + (RollPower - 25)

是什么原因导致此错误以及如何修复它?

编辑:我已将数据定义为全局变量,现在的错误是

File "Raspberry_pi.py", line 39

return data

    ^

SyntaxError: invalid syntax

最佳答案

您的代码中存在许多语法问题。由于 SyntaxError 的性质异常(当解释器不理解代码语法时引发)时,错误消息可能无法将正确的行识别为问题的根源。

我看到的第一个语法错误是您正在使用 breakGetMessage函数而不处于循环中。一个break语句仅在 for 内有用或while block ,并在其他地方使用它(在本例中为 except block )是一个语法错误。

下一组错误与缺少冒号有关。 DecodeInput 中的每个条件分支和SetPower条件后需要有冒号:if condition1: , elif condition2: , else:

使用 else if 也是一个错误而不是elif (如果在 else: 之后添加冒号、换行符和额外的缩进,然后使用单独的 if 语句,则可以使其工作,但这会浪费空间)。

还有一些其他问题,但它们不是语法错误。例如,您在定义函数之前从顶级代码调用函数,并且 DecodeInput有一行带有裸露表达式 SetPower它没有做任何有用的事情(您可能想通过一些参数调用 SetPower)。

希望这能让您走上正轨。

关于Python 返回无效语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29185596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com