Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python error
#1
Hello guys,

One small help

I have written a code

print('Temp={0:0.1f}*C Humidity={1:0.1f}%'.format(temperature, humidity))

and it is giving me an error

Unknown format code 'f' for object of type 'str'

I tried float (temperature), float (temperature) even but it doesn't seems to work.

Kindly help.
Reply
#2
(Aug-12-2018, 03:49 AM)Prashant93 Wrote: I tried float (temperature), float (temperature) even but it doesn't seems to work.
the error is clear. you need numeric value when using format code f in formating string.
show us minimal, runnable snippet that reproduce the error.
How to create a Minimal, Complete, and Verifiable example
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
#!/usr/bin/python
import sys
import Adafruit_DHT

while True:

    humidity, temperature = Adafruit_DHT.read_retry(11, 4)

    print 'Temp: {0:0.1f} C  Humidity: {1:0.1f} %'.format(temperature, humidity)
---Here I am facing problem
Reply
#4
Adafruit_DHT.read_retry() will return either tuple of floats or tuple of None's, i.e. (None, None) if it is not able to get correct reading from the sensor. In your case it returns (None, None)
for some reason it's not able to get good reading from your sensor.

print 'Temp: {0:0.1f} C Humidity: {1:0.1f} %'.format(None, None)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: Unknown format code 'f' for object of type 'str'
the docstrin for read_retry:
Quote:"""Read DHT sensor of specified sensor type (DHT11, DHT22, or AM2302) on
specified pin and return a tuple of humidity (as a floating point value
in percent) and temperature (as a floating point value in Celsius).
Unlike the read function, this read_retry function will attempt to read
multiple times (up to the specified max retries) until a good reading can be
found. If a good reading cannot be found after the amount of retries, a tuple
of (None, None) is returned. The delay between retries is by default 2
seconds, but can be overridden.
"""

By the way, why do you use python2? In python3 you will get
>>> print('Temp: {0:0.1f} C Humidity: {1:0.1f} %'.format(None, None))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: non-empty format string passed to object.__format__
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Actually, I am using Grove Pi and my sensor is on port D4, Does that matter?
Reply
#6
(Aug-12-2018, 05:37 PM)Prashant93 Wrote: Actually, I am using Grove Pi and my sensor is on port D4, Does that matter?
I have no idea. But it looks like you don't get good readings from it
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Hi, I tried using python 3. I am getting the same error, How can I fix this??

TypeError: non-empty format string passed to object.__format__
Reply
#8
it looks they have their own code base
https://github.com/DexterInd/GrovePi
there are installation instruction, example code and project.
Note that they import gruvipi (I expect it is installed when you install from their repository)
I don't know if Adafruit_DHT can handle their sensors correctly.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
(Aug-12-2018, 06:12 PM)Prashant93 Wrote: Hi, I tried using python 3. I am getting the same error, How can I fix this??

TypeError: non-empty format string passed to object.__format__
The problem is not in the code, but because you don't get good readings, thus it returns None, None
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#10
Hi I am using grovepi code for sensors and want to store in sqlite3 and so I am using this code below:
import grovepi
import time
import math
dbname='sensorsData.db'
import sqlite3

sampleFreq=1*60
def getDHTdata():
blue=0
sensor = 4  # The Sensor goes on digital port 4.



while True:
    try:
        # This example uses the blue colored sensor.
        # The first parameter is the port, the second parameter is the type of sensor.
        [temp,humidity] = grovepi.dht(sensor,blue)  
        if math.isnan(temp) == False and math.isnan(humidity) == False:
            print("temp = %.02f C humidity =%.02f%%"%(temp, humidity))

    except IOError:
        print ("Error")
I am getting error name sensor is not defined. Please help.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020