Cisco数据包跟踪程序错误python中的UnboundLocalError

2024-09-30 05:19:23 发布

您现在位置:Python中文网/ 问答频道 /正文

根据大多数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

系统:System

代码图像

enter image description here


Tags: 代码fromimportdataifmaindef错误

热门问题