根据大多数python用户的说法,在运行python代码时,我会遇到一个UnboundLocalError
错误。packettracer的版本是8.0.0,可能是python2
有人告诉我,全球化不适合使用,相反,我应该使用Ireturn
值所在的函数
代码:
from udp import *
from time import *
from gpio import *
def onUDPReceive(ip, port, data):
return int(data)
def main():
socket = UDPSocket()
temp=socket.onReceive(onUDPReceive)
print(socket.begin(1235))
while True:
if temp >= 520:#error
digitalWrite(3, HIGH);
delay(1000)
digitalWrite(3, LOW);
delay(500)
customWrite(9, 2);
else:
digitalWrite(0, LOW);
customWrite(2, 0);
if __name__ == "__main__":
main()
错误:UnboundLocalError: local variable 'temp' referenced before assignment in file main.py on line 15
我尝试过的:
我已经厌倦了在temp=socket.onReceive(onUDPReceive)
之后立即print(temp);
,但它仍然给我同样的错误
添加时
try:
print(temp);
ParseError: bad input in file main.py on line 17
代码图像
目前没有回答
相关问题 更多 >
编程相关推荐