How to convert a value to float and int in python?

Asked

Viewed 210 times

0

Hello, I am trying to convert a received value from a graphical interface to int or float, to make a software that solves the second degree equations.

layout = [  [sg.Text('Termo A:'), sg.InputText()], #os valores colocados aqui devem ser convertidos para float ou int
        [sg.Text('Termo B:'), sg.InputText()],
        [sg.Text('Termo C:'), sg.InputText()],
        [sg.Ok(), sg.Cancel()]] 

I have tried several things to convert these values, but none worked.

elif event in (sg.ok, 'Ok'):
      valor_float = float(values)
      print (type(valor_float[0]))

In the above example of the following error:

Typeerror: float() argument must be a string or a number, not 'Dict'

I am using the Pysimplegui library. Thanks in advance.

  • 2

    But it won’t work anyway. The value you are trying to convert, according to the error message, is a dictionary. Probably the target value of this conversion should be inside this dictionary.

  • Hello Elisha, it is important [Dit] your question and explain objectively and punctually the difficulty found, accompanied by a [mcve] of the problem. To better enjoy the site, understand and avoid closures is worth reading the Stack Overflow Survival Guide in English. So we can help. Thank you for understanding.

2 answers

0

valor_float = float(valor)
valor_int = int(valor)
  • Hello, I had already tried this, it appears like this "Typeerror: float() argument must be a string or a number, not 'Dict", I appreciate the comment

  • @Eliseuperes this error appears because the float and the int accept only numbers or strings for conversion. They do not accept a dictionary or any other object.

  • 1

    So why can’t you even accept - what would be the point of converting a dictionary, which is a map of names to values, to a number? In the question is not displayed where comes "values" or what it is - this error message says it is a dictionary You need to know what inside the dictionary you want to turn into float or integer, and make the equivalent call by passing this value.

0


Your specific error you are trying to convert a dictionary to a float.

In this case you need to access the value of your dictionary so you can then convert its values.

For example:

dicionario = { 'Termo A:':  '1234', 'Termo B:': '2.4','Termo C': '0.66'}

for x in dicionario.values():
    valor_float = float(x)
    print (type(valor_float))

values() reads returns all values from your dictionary, so you need to use a loop to print each value, you can also access the specified index.

valor_float = float(dicionario['Termo B:'])
print (type(valor_float))
print(dicionario['Termo B:'])

You can still access your key/value

for chave, valor in dicionario.items():
    if chave == 'Termo B:':
        valor_float = float(dicionario['Termo B:'])
        print (type(valor_float))
        print(f'{chave} Seu valor é {valor}')

To convert to int just use the same logic and change the float to int.

Browser other questions tagged

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