【问题标题】:AttributeError: 'NoneType' object has no attribute 'split' in python when I rune my codeAttributeError:当我运行我的代码时,'NoneType' 对象在 python 中没有属性'split'
【发布时间】:2021-03-27 18:17:27
【问题描述】:
from microbit import *
import radio

radio.on()  
radio.config(channel=8)
routing_table = { 'Alice': 8, 'Bob': 10, 'Charlie': 15 }
spy = 60

def forward_message(msg):
    source, destination, payload = msg.split(':')
    radio.config(channel=10)
    radio.send(msg)

def forward_to_spy(msg):
    source, destination, payload = msg.split(':')
    radio.config(channel=60)
    radio.send(msg)

while True:
    msg = radio.receive()
    if msg:
        msg = radio.receive()
        forward_message(msg)
        forward_to_spy(msg)
        ack = radio.receive()
        while ack is None:
            ack = radio.receive()
        forward_message(ack)

每当我提交此代码时,它都会出现该错误,请有人帮助我。我正在尝试接收消息,然后将它们发送给他们需要去的人,同时还将消息发送给间谍,以参加 grok 课程。我已经坚持了很长时间了,所以再次请有人帮忙。

【问题讨论】:

    标签: python radio grok


    【解决方案1】:

    您正在检查该消息,但随后在下一行收到另一条消息,而不是检查它是否为 None

      msg = radio.receive()
      if msg:
        msg = radio.receive()
        forward_message(msg)
    

    所以您可能将None 传递给forward_message 并尝试在None 上使用splitNone 没有导致此错误的 split 方法。

    删除第二个msg = radio.receive(),这样您就只会在检查if msg 后传递消息。

      msg = radio.receive()
      if msg:
        forward_message(msg)
    

    【讨论】:

      猜你喜欢
      • 2016-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-13
      • 1970-01-01
      • 2022-11-13
      • 1970-01-01
      • 2022-07-17
      相关资源
      最近更新 更多