Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我在python中的计算有问题_Python - Fatal编程技术网

我在python中的计算有问题

我在python中的计算有问题,python,Python,我正在尝试学习python,作为一个测试,我试图制作一个BMI计算器 #BMI Calculator name = raw_input ("What is your name?: ") weight = raw_input ("Hello %s What is your weight? (kilo): "% (name)) height = raw_input ("And what is your height? (cm) ") #Calculations: weight / height^

我正在尝试学习python,作为一个测试,我试图制作一个BMI计算器

#BMI Calculator

name = raw_input ("What is your name?: ")
weight = raw_input ("Hello %s What is your weight? (kilo): "% (name))
height = raw_input ("And what is your height? (cm) ")

#Calculations: weight / height^2 * 10000

weight = int(weight)
height = int(height)

BMI = (weight / height ** 2) * 10000

print "%s, your BMI is %s"% (name, BMI)

但是我的计算似乎有问题,因为我的BMI总是为0?怎么了?

在Python和几种语言中,
/
是“整数除法”的运算符。例如,在Python中,请尝试:

num = 1 / 2 # Comes out to be 0
相反,请确保除法计算中涉及的数字之一是浮点数:

num = 1.0 / 2 # Comes out to be 0.5!
在您的场景中,由于
height
weight
是整数,因此它意外地进行了整数除法

您还可以将变量强制转换为浮动,如下所示:

weight_f = float(weight)

在Python和几种语言中,
/
是“整数除法”的运算符。例如,在Python中,请尝试:

num = 1 / 2 # Comes out to be 0
相反,请确保除法计算中涉及的数字之一是浮点数:

num = 1.0 / 2 # Comes out to be 0.5!
在您的场景中,由于
height
weight
是整数,因此它意外地进行了整数除法

您还可以将变量强制转换为浮动,如下所示:

weight_f = float(weight)

我猜你在用Python 2吧?除非你是病态肥胖,
height
将大于
weight
,因此根据dup,答案将为零。更改此行BMI=(weight/height**2)*float(10000)甚至更好的BMI=(weight/float(height)**2)*10000我想你在使用Python 2吧?除非你是病态肥胖,
身高
将大于
体重
,因此根据dup,答案将为零。更改此行BMI=(体重/身高**2)*浮动(10000)甚至更好的BMI=(体重/浮动(身高)**2)*10000