正如你可以从下面的代码中看出的那样,我一直在试图获得一个浮点结果,但是每次我在数字中打拳击,它总是给我一个int。任何帮助,将不胜感激。如何获得浮动结果? (Python)
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)))
试试这个:http://stackoverflow.com/questions/1267869/我怎样才能在i中划分浮点运算? – Marco
您可以定义高度和宽度,然后使用x和y作为结果......为什么?尝试使用宽度和高度。 – Shovalt
哦,嘿,你用%d代表数字!你需要%f或%.2f,如果你想很好地显示 – Shovalt