-1

I'm using the Adafruit DHT11 Library for my temperature sensor. Sometimes the value that I get form that reading can't be converted to a float (I really don't know what kind of var type that is it only comes out every like 50 readings). Whenever this happens my script stops working with the error code: float() argument must be a string or a number. The piece of code where this would be important is:

mid = sum(list)/len(list)
print mid
humidity, temperature = Adafruit_DHT.read_retry(11,4)
temp = float(temperature)
if mid + 3 > temp > mid - 3:  
    del list[0]
    print 'all safe %d' % (temp)
    list.append(temp) 

Isn't there some way to make an if expression where the temperature variable is tested before the conversion. The result if it isn't convertible would just be to skip the whole code and start at the beginning for a new reading.

Chris Catignani
  • 5,040
  • 16
  • 42
  • 49
  • you should format the code snippet as code. highlight it and click the '{ }' symbol. – ColonelFazackerley Mar 07 '18 at 09:57
  • 1
    For further reading: https://stackoverflow.com/q/12265451/1025391 – moooeeeep Mar 07 '18 at 10:00
  • Does this answer your question? [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – Tomerikoo Jan 10 '21 at 20:22

3 Answers3

1

You should use a try-except, where you try to do the conversion to float and if it raises the error it just ignores.

The relevant part would become

try:  # trying the next block
    temp = float(temperature)
    if mid + 3 > temp > mid - 3:  
        del list[0]
        print 'all safe %d' % (temp)
        list.append(temp) 
except ValueError:  # if ValueError raises, then you ignore that reading
    pass
Ignacio Vergara Kausel
  • 5,521
  • 4
  • 31
  • 41
  • It's good practice to minimise the amount of stuff in the try block. If there is too much it increases the chance that what you catch does not come from where you think. – ColonelFazackerley Mar 07 '18 at 10:03
  • Thanks i think i'm gonna use your method since its the cleanest way for me and i maybe need to implement it later in the code again – Florian Hanak-Hammerl Mar 07 '18 at 10:15
  • @ColonelFazackerley I agree, but the `if` statement depends on the success of the `temp` assignment. – Ignacio Vergara Kausel Mar 07 '18 at 10:27
  • @IgnacioVergaraKausel I assume this snippet is in a loop or function. I would put the `float()` call alone in the `try:` block, and then either `continue` (loop) or `return` (function) in the `except:` block. – ColonelFazackerley Mar 07 '18 at 11:13
  • @ColonelFazackerley yes, that sounds like a good solution to me... and how most likely I'd do it myself. I'm just not assuming anything beyond what it's shown in the question. – Ignacio Vergara Kausel Mar 07 '18 at 11:16
0

The usual way to handle this in python is to catch the error.

try:
   ham = float("spam")
except ValueError:
   print("could not parse to float")
0

There is no way in python stdlib. You can write your function liek:

def val_float(float_string):
    try:
        temp = float(float_string)
    except ValueError:
        return False
    return True

if val_float(temperature):
    # do your stuff.
Rahul
  • 10,830
  • 4
  • 53
  • 88