[英]variable referenced before assignment: Python
我正在嘗試從樹莓派3到Azure的數據
該腳本從Raspberry Pi3讀取數據,Raspberry Pi3通過藍牙連接到傳感器並獲取多個值。
不幸的是,我在運行時遇到錯誤
“分配前已引用局部變量'溫度'”
def iothub_client_sample_run():
msgs=[]
for address, name in list(devices.items()):
try:
client = iothub_client_init()
if client.protocol == IoTHubTransportProvider.MQTT & (name == "Flower care"):
msg_txt_formatted = MSG_TXT % (
temperature,
sunlight,
moisture,
fertility)
message = IoTHubMessage(msg_txt_formatted)
# optional: assign ids
message.temperature_id = "%d" % temperature
client.send_event_async(message, send_confirmation_callback, devices.items())
print ( "IoTHubClient.send_event_async accepted message {} for transmission to IoT Hub.".format(devices.items()) )
return msgs
while 1:
msgs=iothub_client_sample_run()
for msg in msgs:
print msg['topic']
print msg['payload']
(result, mid)=mqttc.publish(msg['topic'],msg['payload'])
print ( "Send status: %s" % status )
time.sleep(10)
mqttc.disconnect()
except IoTHubError as iothub_error:
print ( "Unexpected error %s from IoTHub" % iothub_error )
return
except KeyboardInterrupt:
print ( "IoTHubClient sample stopped" )
print_last_message_time(client)
錯誤消息很清楚。
請記住,Python一次只能讀取和執行一行代碼,因此,如果在使用它的函數后有一個聲明的變量,那么它將拋出錯誤。 仔細處理代碼,以在調用變量之前放入變量,並且此錯誤不會再有任何問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.