本文目錄一覽:
- 1、怎樣用python計算bmi
- 2、Python小白一枚,自己寫的BMI指數計算器,求教高手一下代碼如何重複輸入以及如何結束循環?
- 3、Python計算BMI值
- 4、求一道Python題,是關於定義函數和身體指數的,謝謝各位大神啦!!!
怎樣用python計算bmi
weight=int(raw_input(“請輸入體重(千克):”))
height=int(raw_input(“請輸入身高(米):”))
BMI=weight/(height*height)
print “BMI=”,BMI
if BMI19:
print「輕體重”
elif BMI=19 and BMI25:
print”健康身體”
elif BMI=25 and BMI28:
print”超重「
else:
print”肥胖」
raw_input(“press any key to quit.”)
Python小白一枚,自己寫的BMI指數計算器,求教高手一下代碼如何重複輸入以及如何結束循環?
想讓程序循環,在最外層套一個while就可以了,想跳出的時候寫break就可以了。
在你的代碼中,while不該套在if外面,其次像你這樣判斷直接用if,就可以了,不需要elseif,直接if效率更高。
想跳出,只要寫條件執行break就行,比如下面我的代碼中,輸入N就結束,輸入Y就繼續。
while(1):
print(‘Welcome to use calculator of BMI exponent for human:’)
w = float(input(‘Please enter your weight(kg):’))
h = float(input(‘Please enter your height(m):’))
BMI = w / (h * h)
if BMI 18.5: print(‘you are thin !’)
if 18.5 = BMI =24.9: print(‘you are normal !’)
if BMI =25: print(‘you are little fat !’)
if 25.0 BMI =29.9: print(‘you are more little fat !’)
if 30.0 = BMI = 34.9: print(‘you are fat !’)
if 35.0 = BMI =39.9: print(‘you are serious fat !’)
if BMI =40: print(‘you are extreme fat !’)
print(‘continue?Y/N’)
n=”
while(n!=’Y’ and n!=’N’):
n=input()
if(n==’N’):
break
Python計算BMI值
廢話不多說,改進版繼續上程序哈:
def bmi():
name=input(‘Name:’)
height=input(‘Height(m):’)
weight=input(‘Weight(kg):’)
BIM=float(float(weight)/(float(height)**2))
print(‘您的BIM指數為:’,BIM)
if BIM 18.5:
print(‘你太輕了!’)
elif BIM=25:
print(‘標準體重哦哦!’)
elif BIM=32:
print(‘您有點微胖哦哦!’)
else:
print(‘您太胖了,該減肥了’)
bmi()
for i in range(10):
choose =input(‘您是否願意繼續計算BMI(y/n):’)
if choose==’y’:
bmi()
else:
break
求一道Python題,是關於定義函數和身體指數的,謝謝各位大神啦!!!
按照題目要求編寫的Python程序如下
def calBMI(height,weight):
BMI=weight/(height*height)
if BMI18.5:
return [BMI,”過輕”]
elif BMI24:
return [BMI,”正常”]
elif BMI28:
return [BMI,”過重”]
else:
return [BMI,”肥胖”]
import re
s=input(“請輸入你的身高(米)和體重(公斤)【逗號隔開】:”)
s1=re.split(r'[,,]’,s)
height=float(s1[0])
weight=float(s1[1])
name=”李子健”
bmi=calBMI(height,weight)
print(“{}的測算結果為:”.format(name))
print(“BMI:%.2f”%bmi[0])
print(bmi[1])
源代碼(注意源代碼的縮進)
原創文章,作者:BXXOQ,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/128694.html