This forum is now read-only. Please use our new forums! Go to forums

0 points
Submitted by 21dannyg
over 9 years

Tip rate error

I wrote tip = 15/100 and I get the error message

Oops, try again. It looks like tip is set to 0 instead of 0.15.

I then typed tip = 15.0/100 and it worked.

Do I have to type in .0 and if so why?

Answer 540f2b2980ff33692d0009b6

3 votes

Permalink

For Python version 2.x, / performs integer division, if both operands are ints. So you need to make sure at least one of them is a float prior to performing the division, if you need float division.

Other means of doing this are …

tip = 15 / float(100)

.. or …

tip = float(15) / 100
points
Submitted by Glenn Richard
over 9 years

2 comments

vasanthraj1 over 9 years

nice

biogon over 9 years

@Glenn Richard - thank you! That was a very strange thing that the tutorial doesn’t explain.

Answer 540f35ff52f8636916000bd7

0 votes

Permalink

Thanks for response -but I don’t understand?????

Why can you not simply do 15/100 - both are integers???????

Not sure of your terminology????

points
Submitted by 21dannyg
over 9 years

1 comments

Mike Lewry over 9 years

Since both 15 and 100 are integers Python 2.x will round the answer down to an integer too. So 0.15 rounds down to zero. Making at least one of the numbers a non-integer ensures Python doesn’t round the answer.