python - Python 中的类型错误 : unsupported types for : 'int' , 'NoneType'

标签 python micropython bbc-microbit

我正在为客户端编写一个程序,该程序通过客户端记录的内置平均值 radio 从服务器接收平均值字符串。我收到一个 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:

最佳答案

试试这个:

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 来检查 incoming 是否为 None。但是,您在之前的缩进级别上有以下代码块,因此,当 incomingNone 时,这些代码块也会执行。

关于python - Python 中的类型错误 : unsupported types for : 'int' , 'NoneType',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41987956/

相关文章:

python - 在 Python 中复制 SQL 的 'Join'

python - MUSIC算法Spectrum Python实现

python - 如何更新列表中没有 "get"的单元格?

raspberry-pi - Raspberry pi pico rfid rc522(Micropython)

micropython - esptool.py 未被识别为内部或外部命令、可运行程序或批处理文件

bbc-microbit - Microbit 可以打开 LED,但不能打开电机

c++ - 微:Bit read bluetooth message

python - 在 CentOs 上安装 pygit2 时出错

pypi - 无法使用upip在esp32上安装uasyncio包