Reputation: 2777
I'm a programming newbie having difficulty with Python multiplication. I have code like this:
def getPriceDiscount():
price = int(input())
if price > 3000:
priceDiscount = price * 0.6
return priceDiscount
else:
return price
But when I execute it and type an input which is a decimal number like 87.94, I get the following error:
ValueError: invalid literal for int() with base 10: '87.94'
Isn't the int()
method able to convert the string '87.94' into a number allowing me to multiply it by 0.6? What should I do to perform that conversion?
I'm using Python 3.2.2.
Upvotes: 2
Views: 23962
Reputation: 29302
The problem is that you're trying to do two convertions at a time, and one of them is implicit.
The following will work because there's an obvious way to transfrom the number '87' to the integer 87.
>>> int('87')
87
And for the same reason, the following will work too:
>>> float('87')
87.0
>>> float('87.94')
87.94
>>> int(87.94)
87
Keep in mind what's been said and look at the difference between:
>>> int(float('87.94'))
87
and
>>> int('87.94')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '87.94'
As you see the problem is that you're implying a float()
conversion before passing to int()
, but how could the compiler guess that? There are plenty of available working conversions for the string '87.94'. Should the compiler tried them all before finding out which onw will work with int
? And what if there are two or more that will return something that will work with integer?
Examples:
>>> int(hash('87.94'))
4165905645189346193
>>> int(id('87.94'))
22325264
>>> int(max('87.94'))
9
>>> int(input('87.94'))
87.94111
111
Should the compiler choose float()
for you?
I don't think so: Explicit is better than implicit.
Anyway if you are going to use that number to perform a multiplication with a float number 0.6
. You could directly convert it to a float()
.
Change this line:
price = int(input())
with:
price = float(input())
And everything will be ok.
Also, the operation you are presenting in your example is a multiplication, not a division. In case you might be interested, there is the floor division //
that will return an integer insted of a float. Take a look at PEP238 for more informations about this.
Upvotes: 1
Reputation: 6177
An int
(short for "integer") is a whole number. A float
(short for "floating-point number") is a number with a decimal point.
int()
returns an int
created from its input argument. You can use it to convert a string like "15"
into the int
15
, or a float
like 12.059
into the int
12
.
float()
returns a float
created from its input argument. You can use it to convert a string like "10.5"
into the float
10.5
, or even an int
like 12
into the float
12.0
.
If you want to force price
to be an integer, but you want to accept a floating point number as typed input, you need to make the input a float
first, then convert it with int()
:
price = int(float(input())
Note that if you multiply an int
by a float
such as your discount factor, the result will be a float
.
Also note that my example above doesn't round the number -- it just truncates the stuff after the decimal point. For example, if the input is "0.6"
then price
will end up being 0
. Not what you want? Then you'll need to use the round()
function on the float
first.
price = int(round(float(input()))
If you intended to use floating point calculations (which makes sense if we're talking about a commodity price), then don't perform the int
conversion. Just use float
. You may still want to do some rounding. If you want to round to 2 decimal places, you can call round with the second argument as 2
:
price = round(float(input()),2)
Finally, you might want to look into Python's decimal
module, since there are limitations when using floating point numbers. See here for more information: http://docs.python.org/tutorial/floatingpoint.html
Upvotes: 1
Reputation: 1021
def getPriceDiscount():
while True:
try:
price, percent = float(input()), 0.6
break
except ValueError:
continue
return price * percent if price > 3000 else price
print(getPriceDiscount())
Upvotes: 1
Reputation: 38970
You can't pass a string with a decimal point to int()
. You can use float()
instead. If you want only the integral part i.e. to truncate the .94
in 87.94
, you can do int(float('87.94'))
.
Upvotes: 4
Reputation: 20915
Actually, you CAN pass a floating point value to int()
. In that case int()
just rounds the number down and converts that value to an integer type.
However, what you're doing when you call int("87.94")
is passing a string resembling a decimal point value to int()
. int()
can't directly convert from such a string to an integer. You hence must use float()
, which can convert from strings to floats.
Upvotes: 6