Could not convert string to float. Why?

Asked

Viewed 13,516 times

2

I tried to convert some values to float

self.total_PreçoCusto =   float( self.PreçoCusto) * float(self.estoque)
self.total_Preço_Venda =  float( self.Preço_Venda) *  float(self.estoque)
self.lucro =  float(self.Preço_Venda-self.PreçoCusto) 

But gave error saying that could not convert

Valueerror: could not Convert string to float:

2 answers

7

This exception is made when the string that you are trying to convert is not in a valid format. This happens when you use the comma as decimal separator, when the string is empty or has non-numeric characters, etc.

>>> float('3,14159')
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    float('3,14159')
ValueError: could not convert string to float: 3,14159

>>> float('')
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    float('3,14159')
ValueError: could not convert string to float: 

>>> float('foo')
Traceback (most recent call last):
  File "main.py", line 1, in <module>
    float('3,14159')
ValueError: could not convert string to float: foo

If you are using the comma, you need to replace the comma with the dot.

>>> float('3,14159'.replace(',', '.'))
3.14159

In other situations, you will need to validate first before converting to float. If the string is empty or has invalid characters, you must treat directly in the source of values. There is no way to define a generic solution.

Other than that, it is interesting that you keep a pattern in the nomenclatures of your variables. By convention, Python uses the Snake case, using the underlining, _, as a word separator. So instead of total_PreçoCusto, prefer total_preço_custo, as well as preço_custo and preço_venda.

-3

Remove the ç. Pricecost is not a valid variable, it will cause a number of errors. Preferably use only accented letters, digits and underscore to declare variable names.

Moreover if self.PrecoCusto and self.Preco_Venda are not float, can not do self.Preco_Venda - self.PrecoCusto, would have to be

self.lucro =  float(self.Preco_Venda) - float(self.PrecoCusto)

or maybe what you really want is

self.lucro = total_Preco_Venda - total_PrecoCusto
  • 4

    It turns out that Python, as well as several other languages, accepts accents, cedilla and etc in variable names. Finally, the error described in the question does not refer to this.

  • @Lipespry actually didn’t even say which error he found.

  • 3

    Read the title of the question.

  • hello folks the error I found was this : could not Convert string to float: I changed the code, but it keeps popping up, help, pfvr: File "C: exdb pp.py", line 179, in itens_get self.totalcp = float(self.cp) * float(self.stock) Valueerror: could not Convert string to float:

  • self.totalcp = float(self.cp) * float(self.stock) self.totalsp = float(self.sp) ? float(self.stock) self.profit = float(self.sp ) - (self.cp)

Browser other questions tagged

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