As you can see in official documentation:
class int([x]) / class int(x, base=10)
Return an integer Object constructed from a number or string x
, or Return 0 if no Arguments are Given. If x
defines __int__()
, int(x)
Returns x.__int__()
. If x
defines __trunc__()
, it Returns x.__trunc__()
. For floating point Numbers, this Truncates Towards zero.
If x
is not a number or if base is Given, then x
must be a string, bytes, or bytearray instance Representing an integer literal in Radix base.
The parameter can be a number or a string. If the parameter implements the method __trunc__()
, the value will be returned x.__trunc__()
, which is the case when you pass a number with floating point.
If the parameter defines __int__()
then you will be returned x.__int__()
.
If the parameter is of type string, bytes or bytearray, then the conversion will be made according to the informed base, which by default is 10, decimal base, which we use in day-to-day. When passing a figure like '1.93'
, due to the presence of quotation marks, this will be a string and therefore the conversion of that string to integer in base 10 will be attempted. As the value 1.93
is invalid in base 10 (because the character .
does not exist in base 10), gives the cited error.
But this was only to explain why the error, because the solution is simpler than this, given that it is a logic error even, since a decimal input is expected and is being treated as integer. Just. Then, treat it properly as decimal. For this, one of the ways to do is by using float
.
An alternative is to use the library decimal
, python native.
Yes. The value "1.93" is not a valid integer value, so it is expected to give the cited error.
– Woss
how can I write to put this broken value?
– orbitB374
Don’t treat him as whole... maybe as
float
.– Woss
@orbitB374 Use the type
float
.– Jéf Bueno
thank you guys :) I’m new in python, so I’m learning everything yet
– orbitB374