0

I'm having some trouble running the code I wrote.

meal = float(raw_input("How much did your meal cost? "))

tax = 6.75 / 100
tip = 15 / 100

total = (meal * tax) * tip

print total

You can see above that I made this "tip calculator". Whenever I enter a number, it returns with zeros. It seems like it's skipping the entire calculation part.

Any solution?

khelwood
  • 55,782
  • 14
  • 81
  • 108
Henrik Berg
  • 105
  • 1
  • 1
  • 10

3 Answers3

3

If it is python 2.7 you are doing integer division, which means your tip is calculating as 15/100 = 0.

from __future__ import division

At the the top of the program will solve this.

draco_alpine
  • 769
  • 11
  • 25
1

In python 2, / is integer division. Thus, 15 / 100 = 0, and you miltiply by that. Just replace 15 with 15.0

Dmitry Torba
  • 3,004
  • 1
  • 14
  • 24
  • That's not quite true. In Python 2, `/` performs integer division if (& only if) both of its operands are integers (unless you do `from __future__ import division`). – PM 2Ring Aug 23 '16 at 14:09
0

The problem is on this line:

tip = 15 / 100

Dividing an int by an int will result in an int. In this case 15 / 100 gives you 0, and the complementary modulo operation 15 % 100 gives the 15 remainder.

If you change 15 to a float it will work because precision is preserved and tip will be a float instead of an int. Try:

tip = 15.0 / 100

doykle
  • 127
  • 7