简体   繁体   English

Python舍入变量打印0

[英]Python Rounded Variable print 0

I have the next snippet in a program: 我在程序中有下一个代码段:

#weight in kg
weight = float(input('Enter your weight: '))
#height in cm
height = float(input('Enter your height: '))

bmi_metric = weight/(height**2)/(100**2)
print("{:.2f}".format(bmi_metric))

The problem is when I try to round even with round() function it spits 0 in console. 问题是,当我尝试使用round()函数舍入时,它在控制台中吐出0。 If I let unformatted it gives me the right number with a lot of decimals. 如果我不格式化,它会给我正确的数字,并带有许多小数。 This is the console: 这是控制台:

Enter your weight: 44
Enter your height: 182
0.000

Everything works fine (bmi_metric in your case is super small, so rounding gives 0 indeed), but you have a wrong equation for BMI calculation: 一切正常(您的情况下bmi_metric非常小,因此舍入实际上为0),但是BMI计算公式有误:

#weight in kg
weight = float(input('Enter your weight: '))
#height in cm
height = float(input('Enter your height: '))

bmi_metric = weight/((height/100)**2)
print("{:.2f}".format(bmi_metric))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM