【问题标题】:String not updating with changing variable字符串不随变量变化而更新
【发布时间】:2020-10-13 18:08:06
【问题描述】:

我正在尝试使用每次发送消息时都会更新的变量来更新字符串 (MESSAGE) (msg_no)

我查了一下,似乎 msg_no 变量正在按照我想要的方式更新,但字符串 (MESSAGE) 没有更新(每次都显示为 0)。我不知道为什么,谢谢你的帮助。

import xbee
import time

msg_no = 0

# TODO: replace with the 64-bit address of your target device.
TARGET_64BIT_ADDR = b'\x00\x13\xA2\x00\x41\xB7\x6C\x8C'
MESSAGE = "Hello from router (Msg No. %s)" % (msg_no)

print(" +---------------------------------------+")
print(" | XBee MicroPython Transmit Data Sample |")
print(" +---------------------------------------+\n")

print("Sending data to %s >> %s" % (''.join('{:02x}'.format(x).upper() for x in TARGET_64BIT_ADDR),
                                    MESSAGE))

try:
    msg_no += 1
    xbee.transmit(TARGET_64BIT_ADDR, MESSAGE)
    print("Data sent successfully")
except Exception as e:
    print("Transmit failure: %s" % str(e))

time.sleep(5)

try:
    msg_no += 1
    MESSAGE = MESSAGE
    xbee.transmit(TARGET_64BIT_ADDR, MESSAGE)
    print("Data sent successfully")
except Exception as e:
    print("Transmit failure: %s" % str(e))

【问题讨论】:

  • 你永远不会更新MESSAGEMESSAGE = MESSAGE 什么都不做
  • ^ 正如 ted 所说,它没有被改变,% msg_no 你还想做什么?你的预期输出是什么...

标签: python xbee


【解决方案1】:

MESSAGE = MESSAGE没有任何作用,你可以做一个消息模板,然后在你想创建消息的时候使用它:

# put this line on top of the script
MESSAGE_TEMPLATE = "Hello from router (Msg No. %s)"

#some code

# use this whenever a message with number `msg_no` needs to be created
MESSAGE = MESSAGE_TEMPLATE % msg_no

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-06
    • 1970-01-01
    • 1970-01-01
    • 2016-10-27
    相关资源
    最近更新 更多