1

我正在为客户端编写一个程序,该程序通过客户端记录的内置平均值无线电从服务器接收平均值字符串。我收到一个 TypeError 说:“不支持的类型:'int','NoneType'”。

这是我的代码:

from microbit import *
import radio

radio.on()

blink = Image("99999:"
              "99999:"
              "99999:"
              "99999:"
              "99999")


radio.send("power on")

while True:
   incoming = radio.receive()
   if incoming is not None:
      incoming = float(incoming)

   sleep(500 - running_time() % 500)
   acceleration = accelerometer.get_z()
   radio.send(str(acceleration))
   acceleration = float(acceleration)

   if acceleration > 2 * incoming or acceleration < .5 * incoming:
       display.show(blink)
       microbit.sleep(150)
       display.clear()
       microbit.sleep(150)

错误发生在这一行:

if acceleration > 2 * incoming or acceleration < .5 * incoming:
4

1 回答 1

2

尝试这个:

from microbit import *
import radio

radio.on()

blink = Image("99999:"
              "99999:"
              "99999:"
              "99999:"
              "99999")


radio.send("power on")

while True:
   incoming = radio.receive()
   if incoming is not None:
      incoming = float(incoming)

      sleep(500 - running_time() % 500)
      acceleration = accelerometer.get_z()
      radio.send(str(acceleration))
      acceleration = float(acceleration)

      if acceleration > 2 * incoming or acceleration < .5 * incoming:
          display.show(blink)
          microbit.sleep(150)
          display.clear()
          microbit.sleep(150)

理由:您的代码(如发布的)首先执行if以检查是否incomingNone. 但是,您在之前的缩进级别有以下代码块,因此,这些代码块也会在incomingis时执行None

于 2017-02-01T19:26:30.320 回答