Python does not advance and does not present an error after converting string to float, how to resolve?

Asked

Viewed 72 times

-3

I’m getting the string 24.120185722369005 from a MQTT(Hivemq) topic, but when converting to float, it ignores the rest of the function.

def on_message(client, userdata, msg):
    
        temperatura = msg.payload.decode()
     
        print('-----------------------------------------------')
        print('NUMERO DE ENTRADA :'+temperatura)
        
        try:
            print('tentando converter')
            temperatura = float(temperatura)

        except ValueError:
            print('bugado')

        #DEPOIS DAQUI ELE NÃO FAZ OS COMANDOS ABAIXO, MAS NÃO MOSTRA ERRO.
    
        print('temperatura convertida: '+temperatura)
    
        print('----------------------------------------------2')
    
        # Validar temperatura de entrada para enviar on/off
        if temperatura > float(50.00):
            print('passando o OFF')
        elif temperatura < float(50.00):
            print('Passando o ONN')

2 answers

0


The error does not occur when trying to convert, the number is converted to float normally. The problem is in these 2 prints: print('NUMERO DE ENTRADA :'+temperatura) and print('temperatura convertida: '+temperatura). The problem is that you cannot concatenate string with float.

The right thing would be: print('NUMERO DE ENTRADA :' + str(temperatura)) and print('temperatura convertida: '+ str(temperatura))

Your code, fixing the problems I said, would look like this:

def on_message(client, userdata, msg):

    temperatura = msg.payload.decode()

    print('-----------------------------------------------')
    print('NUMERO DE ENTRADA :' + str(temperatura))

    try:
        print('tentando converter')
        temperatura = float(temperatura)

    except ValueError:
        print('bugado')

    #DEPOIS DAQUI ELE NÃO FAZ OS COMANDOS ABAIXO, MAS NÃO MOSTRA ERRO.

    print('temperatura convertida: '+ str(temperatura))

    print('----------------------------------------------2')

    # Validar temperatura de entrada para enviar on/off
    if temperatura > float(50.00):
        print('passando o OFF')
    elif temperatura < float(50.00):
        print('Passando o ONN')
  • You have no idea how much you helped Brother, God bless you. I tried it and it worked. I did a lot of research, but I didn’t know where the problem was. Heartfelt thanks.

  • You’re welcome, man. Tmj

0

The problem is that you are concatenating a string with a number, but you can only concatenate with another string.

Only instead of turning the number into a string, as suggested another answer, you can use other options - in my opinion better - to achieve the same result.

The first is using format:

print('temperatura convertida: {}'.format(temperatura))

And the second is using f-string (available for Python >= 3.6):

print(f'temperatura convertida: {temperatura}')
# repare no "f" antes das aspas

Another advantage of these methods is that you can, if you want, use the formatting options (if you want to limit the number of decimals, align right or left, etc).

Learn more by reading here and here.


Another detail, do float(50.00) is redundant and unnecessary. 50.00 is already a number and converting it to number is not necessary. So much so that the code below prints "True":

print(50 == float(50.00)) # True

So just do if temperatura > 50 that is enough.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.