0

As you can probably tell from the code below, I've been trying to get a float result, but everytimes I punch in the numbers, it always gives me an int. Any help would be appreciated.

def wallArea(x, y):
    height = float(x)
    width = float(y)
    result = float(x*y)
    return float(result)

def obsturctionArea(x, y):
    height = float(x)
    width = float(y)
    result = float(x*y)
    return float(result)

litre = float(12.0)

#UserInput
x=float(input("Please enter the height of your wall."))
y=float(input("Please enter the width of your wall."))
a=float(input("Please enter the height of the obtruction."))
b=float(input("Please enter the width of the obtruction."))
coats = float(input("How many coats of paint would you like?"))

totalArea = float(wallArea(x, y)-obsturctionArea(a, b))
result = float(totalArea/litre*coats)

print("You will need %d litres of paint." % (float(result)))
naf
  • 25
  • 4
  • Try this: http://stackoverflow.com/questions/1267869/how-can-i-force-division-to-be-floating-point-in-python – Marco Apr 30 '16 at 15:33
  • You define height and width, but then use x and y for the result... Why is that? Try using width and height. – Shovalt Apr 30 '16 at 15:38
  • Oh, heh, and you use %d, which stands for digit! You need %f, or %.2f if you want to display it nicely – Shovalt Apr 30 '16 at 15:41
  • @Shovalt Didn't know that there was a difference between %d, %f etc, thanks! Works perfectly now! Changed the x and y to width and height, thanks for the heads up! – naf Apr 30 '16 at 15:47

2 Answers2

0
print("You will need %.2f litres of paint." % result)
  • %d - format number as integer
  • %f - format number as float, no limit on number of digits after the decimal point
  • %.xf - x number of digits after the decimal point (e.g. %.2f)
Shovalt
  • 6,407
  • 2
  • 36
  • 51
0

change this line

print("You will need %d litres of paint." % (float(result)))

to

print("You will need %f litres of paint." % (float(result)))

because %d shows the integer value of the variable but %f shows the float one.

you can also specify the number of digits in the float part

for example:

x = 0.123456
print("result = %.2f " % x
# result = 0.12
# %.2f shows only 2 digits in the float part

Also another remark for your code: the variables (height, and width) have no effect in your code, because they aren't used in the function. maybe you wanted to assure the float casting of your variables so If this is what you mean you have to change your functions code to:

def wallArea(x, y):
    height = float(x)
    width = float(y)
    result = float(height*width)
    return float(result)

def obsturctionArea(x, y):
    height = float(x)
    width = float(y)
    result = float(height*width)
    return float(result)
HADID
  • 21
  • 1
  • 5