“ TypeError:格式期望arg 2是字符串或Unicode,而不是浮点数”-实际上我的错误是什么?

时间:2019-05-20 05:59:53

标签: python-3.x raspberry-pi3

当我尝试执行代码时,终端抛出此错误-

Traceback (most recent call last):
  File "Newnotifications.py", line 13, in <module>
    print('Temp={0:0.1f}*C Humidity={1:0.1f}%',format(temperature, humidity))
TypeError: format expects arg 2 to be string or unicode, not float

我试图弄清楚,它似乎很接近,但我无法弄清楚!!

import Adafruit_DHT
from pushbullet import Pushbullet
import requests
import time

sensor=Adafruit_DHT.DHT11

gpio=4

while True:
    humidity, temperature = Adafruit_DHT.read_retry(sensor, gpio)
    if humidity is not None and temperature is not None:
        print('Temp={0:0.1f}*C Humidity={1:0.1f}%',format(temperature, humidity))

    else:
        print('Failed to get reading, Try again!')

    a = str(temperature)
    b = str(humidity)
    pb = Pushbullet("o.WbGFb8CiJvsDral2DDDQGjZKR4vxuvOj")
    push = pb.push_note("Temp="+a+" C","Hum="+b+" %")

    c = "Temp="+a+" C"+"Hum="+b+" %"

    def send_simple_message():
    return requests.post(
        "https://api.mailgun.net/v3/sandboxcb42d20e28604b21916494e622eb45ac.mailgun.org/messages",
        auth=("api", "17163986536c6308836bf42303a11b18-4a62b8e8-ce6717f8"),
        data={"from": "Mailgun Sandbox <postmaster@sandboxcb42d20e28604b21916494e622eb45ac.mailgun.org>",
            "to": "RAJDEEP RAJESH THAKARE <rajdeep.ravithakare@gmail.com>",
            "subject": "Hello from raspberry",
            "text": c})
    send_simple_message()
    time.sleep(10)

这是处决方式:

pi@raspberrypi:~/Desktop $ python Newnotifications.py

Traceback (most recent call last):
  File "Newnotifications.py", line 13, in <module>
    print('Temp={0:0.1f}*C Humidity={1:0.1f}%',format(temperature, humidity))
TypeError: format expects arg 2 to be string or unicode, not float

0 个答案:

没有答案