1. 程式人生 > >python中的條件判斷語句

python中的條件判斷語句

height=input("please input your height,eg.:1.75")
weight=input("please input your weight")
h=int(height)
w=int(weight)
bmi =w/h**2

if bmi <18.5:

    print("too light")
elif bmi<25:
    print("normal")
elif bmi <28:
    print("over weight")
elif bmi <32:
    print("obese")
else:

    print("too fat")

需要注意的幾點:一是運算子的問題h^2在Python中表示為h**2

elseif在Python中為elif,寫成elseif會報錯,eif也報錯

注意在條件判斷語句末尾需要加上冒號,相當於then,忘記寫會報錯

沒有 end if

int語句將輸入的字串格式化為數字。