我如何解决Python中的舍入错误?

问题描述 投票:0回答:1

请参阅下面的代码,我不断获得浮点数的返回而不是四舍五入的整数,我的返回始终是(26.234567901234566)

height = 1.8
weight = 85

heightint = float(height)
weightint = int(weight)

BMI = input(weightint / (heightint ** 2))

BMIint = round(BMI)
print(BMIint)

谢谢, 菲尔

对我的语法和我做错的事情有一些帮助。

python
1个回答
0
投票

您的代码有一些错误。首先,您不应该像您那样使用

input()
函数。另外,身高和体重已经是整数,不需要在前两行之后更改它们。代码的改进版本如下所示:

height = 1.8
weight = 85

# Calculate BMI
BMI = weight / (height ** 2)

# Round BMI to the nearest integer
BMIint = round(BMI)

print("Your BMI is:", BMIint)
© www.soinside.com 2019 - 2024. All rights reserved.