04 python練習習題 身體指數BMI和圓周率

2021-09-25 19:29:23 字數 2119 閱讀 2202

身體指數bmi

國際版

#calbmiv1.py

height, weight = eval(input("請輸入身高(公尺)和體重(公斤)[逗號隔開]: "))

bmi = weight / pow(height, 2)

print("bmi 數值為:".format(bmi))

who = ""

if bmi < 18.5:

who = "偏瘦"

elif 18.5 <= bmi < 25:

who = "正常"

elif 25 <= bmi < 30:

who = "偏胖"

else:

who = "肥胖"

print("bmi 指標為:國際''".format(who))

國內版

#calbmiv2.py

height, weight = eval(input("請輸入身高(公尺)和體重\(公斤)[逗號隔開]: "))

bmi = weight / pow(height, 2)

print("bmi 數值為:".format(bmi))

nat = ""

if bmi < 18.5:

nat = "偏瘦"

elif 18.5 <= bmi < 24:

nat = "正常"

elif 24 <= bmi < 28:

nat = "偏胖"

else:

nat = "肥胖"

print("bmi 指標為:國內''".format(nat))

綜合版

#calbmiv3.py

height, weight = eval(input("請輸入身高(公尺)和體重(公斤)[逗號隔開]: "))

bmi = weight / pow(height, 2)

print("bmi 數值為:".format(bmi))

who, nat = "", ""

if bmi < 18.5:

who, nat = "偏瘦", "偏瘦"

elif 18.5 <= bmi < 24:

who, nat = "正常", "正常"

elif 24 <= bmi < 25:

who, nat = "正常", "偏胖"

elif 25 <= bmi < 28:

who, nat = "偏胖", "偏胖"

elif 28 <= bmi < 30:

who, nat = "偏胖", "肥胖"

else:

who, nat = "肥胖", "肥胖"

print("bmi 指標為:國際'', 國內''".format(who, nat))

圓周率公式法

#calpiv1.py

pi = 0

n = 100

for k in range(n):

pi += 1/pow(16,k)*( \

4/(8*k+1) - 2/(8*k+4) - \

1/(8*k+5) - 1/(8*k+6) ) 

print("圓周率值是: {}".format(pi))

蒙特卡羅方法

#calpiv2.py

from random import random

from time import perf_counter

darts = 1000*1000

hits = 0.0

start = perf_counter()

for i in range(1, darts+1):

x, y = random(), random()

dist = pow(x ** 2 + y ** 2, 0.5)

if dist <= 1.0:

hits = hits + 1

pi = 4 * (hits/darts)

print("圓周率值是: {}".format(pi))

print("執行時間是: s".format(perf_counter() - start))

python練習習題

一 使用if else語句 隨堂練習 使用if else語句 1 年齡在 22到35之間 列印 可以結婚 在其他年齡段 列印 拖出去槍斃 2 年齡在70歲以上 或者 患有老年痴呆 列印不能開車,否則可以開車 練習一 age int input 請輸入年齡 if 22 age 35 print f 你...

python 物件導向程式設計練習習題

請把下面的student物件的gender字段對外隱藏起來,用get gender 和set gender 代替,並檢查引數有效性 gender range male female class student object def init self,name,gender self.name na...

Python 練習題 身體素質指數

要求 不限定函式呼叫時的引數順序,可以身高在前,體重在後,也可以反過來。例 bmi,text calc bmi 身高和體重 請自行設計引數 輸出 您的bmi為xx,您的體型yy bmi 體重 身高的平方def calc bmi height,weight bmi weight height 2 if...