2017-06-02 91 views
0

我想學習Python有毛病我的計算,並作爲測試我試圖做一個BMI計算器在Python

#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?怎麼了?

+0

你使用Python 2,我猜?除非你病態肥胖,否則「身高」將大於「體重」,所以答案將會是零。 –

+0

更改此線BMI =(體重/身高** 2)*浮動(10000) – Eliethesaiyan

+0

甚至更​​好BMI =(體重/浮動(身高)** 2)* 10000 – Eliethesaiyan

回答

2

在Python和幾種語言中/是「整數除法」的運算符。例如,在Python中,嘗試:

num = 1/2 # Comes out to be 0 

相反,確保涉及您的除法計算中的一個號碼是float:

num = 1.0/2 # Comes out to be 0.5! 

在您的方案,因爲heightweight是整數,它意外地在做整數除法!

您也可以施放變量花車,像這樣:

weight_f = float(weight)