前言#
新年快乐呀!隨著春節假期來到尾聲,大家的生活也逐漸回歸正常,這段時間一直閒的沒事,親戚也很少走,我就寫了我一直想寫的 BMI 計算。
什麼是 BMI#
身體質量指數,是 BMI(Body Mass Index)指數,簡稱體質指數,是國際上常用的衡量人體胖瘦程度以及是否健康的一個標準。
計算公式為:BMI = 體重 ÷ 身高 2。(體重單位:千克;身高單位:米。)
BMI 由 19 世紀中期的比利時通才凱特勒最先提出
摘自 百度百科
BMI 在不同國家中,有不同的標準,其中 International Normal Standard 為 20 – 25,此次我才用了 PRC Standard (中國標準),其中 PRC Normal Standard 為 18.5 – 23.9。
輸出預覽#
其中帶 #的部分是我的解釋,正式運行是沒有的
Hello!Nice to meet you what's your name?
Magneto # 我輸入的值
Ok!Magneto. Let's calculate your BMI and classification(adult)
Enter your weight(kg)
42 # 我輸入的值
Enter your height(m)
1.65 # 我輸入的值
Hi Magneto!Your BMI number is 15.426997245179065
Oh!Magneto!You're underweight and BMI is below standerd.
The norm come from PRC
Press Enter to exit
全部代碼#
############################
### Date 2022 January 24 ###
### Author Magneto ###
### Name BMI計算 <——>
### Facility iPad ###
### Language Python ###
############################
#歡迎界面
print("Hello!Nice to meet you what's your name?") # 字串符
the_name = input()
print(f"\nOk!{the_name}. Let's calculate your BMI and classification(adult)") # 字串符
#輸入值
print("\nEnter your weight(kg)")
weight = input()
print("\nEnter your height(m)")
height = input()
#轉化值
your_weight = float(weight)
your_height = float(height)
#計算
number = your_weight/(your_height*your_height)
the_number = float(number)
#得出數值
print(f"\nHi {the_name}!Your BMI number is {the_number} \n") # 字串符
#分析
if the_number < 18.5:
print(f"Oh!{the_name}!You're underweight and BMI is below standerd.")
elif 18.5 <= the_number <= 23.9:
print(f"{the_name},your BMI is normal.")
elif 23.9 < the_number <= 27.9:
print(f"{the_name},you're overweight and BMI is higher than the standard.")
elif 27.9 < the_number <= 32:
print(f"{the_name},you're too fat and BMI well above the mark")
else:
print(f"{the_name},you're severely exeed the limit please lose weight")
print("\nThe norm come from PRC")
x = input('Press Enter to exit')
代碼分析#
分析說明#
代碼分析將會具體講述第幾行的代碼所使用的類型,以及它有什麼用、如何使用,其中行數包含了註釋所佔用的行數。
註釋#
在 Python 中,註釋用 井號 即 # 進行標識。井號後的內容將會被 Python 解釋器 忽略,本程序中 第 1-8 行、第 12 行、第 17 行、第 23 行、第 25 行,均為註釋。
舉例子 #1
#歡迎界面
print("Hello!Nice to meet you what's your name?")
Python 解釋器 將會忽略第一行,只執行第二行
Hello!Nice to meet you what's your name?
註釋有什麼作用?
編寫註釋的主要目的是闡述代碼要做什麼,以及如何做。在開發時,你對各個部分如何協同工作了如指掌,但一段時間後,有些細節你可能會遺忘。當然,你可以通過研究代碼來確定各個部分的工作原理,但通過書寫註釋,可以以清晰的自然語言對此段代碼進行概述,在以後再編寫這段代碼時,將會節省許多時間。
當前,大多數軟件是合作編寫的,編寫者可能是同一家公司的多名員工,有可能是同一個開源項目的開發人員,為了方便大家的協同合作,註釋是必不可少的,因此你最好從一開始就為程序書寫註釋。
用戶輸入#
大多數程序旨在解決最終用戶的問題,為此需要從用戶那裡獲得的一些信息,因此用戶輸入是十分有必要的,而在 Python 中,使用函數 input()
可以有效地解決問題,在本程序中,第10行
、第14行
、第16行
、第37行
使用了本函數。
舉例子 #1
# 基礎 input() 用法
Message = input(“Hello World”)
print(Message)
在執行完該代碼之後,將會顯示以下內容
Hello World
在執行後,程序會等待用戶的輸入,並在用戶按回車後將會繼續運行。輸入值被賦給變量 Message
,接下來的 print (Message)
會將輸入的內容呈現給用戶:
Hello WorldImMagneto
ImMagneto
在正式的程序中,我們很少採用上述的方式,而是使用更清晰的方法,它能夠準確地指出希望用戶提供什麼樣的信息 — 指出用戶應該輸入何種信息
舉例子 #2
name = input(“Please enter your name:”)
print(f”\nHello {name}!”)
在運行和交互後,將會是這樣:
Please enter your name:Magneto
Hello Magneto!
在本程序中,我才用了分離的寫法,讓語句更加清晰。
舉例子 #3
print("Hello!Nice to meet you what's your name?")
the_name = input()
print(f"\nOk!{the_name}. Let's calculate your BMI and classification(adult)")
通過將 print()
和 input()
可以使代碼更加清晰且可觀,
Hello!Nice to meet you what's your name?
Magneto
Ok!Magneto. Let's calculate your BMI and classification(adult)
但在面向用戶的內容上不會有變化,但這在大型項目的開發中是一個不錯的書寫方式,因為如此的寫法可以有效地減少 Bug 的產生,並且只在指定地點調用用戶書寫的內容。
將整數和字串轉換成浮點數#
為了進行必要的運算和顯示,我們必須將將整數和字串轉換成浮點數,因此我們需要引入一個函數 – float()
float () 方法語法
class float([x])
其中 x 代表整數或字串,在運行後,將會返回浮點數。
舉例子 #1
>>>float(1)
1.0
>>> float(112)
112.0
>>> float(-123.6)
-123.6
>>> float('123') # 字串
123.0
在本程序中,需要將浮點數進行計算,並且為了遵循清晰的書寫方法,我們採用分離的寫法
舉例子 #2
#計算
number = 51.2/(1.6*1.6)
the_number = float(number)
#得出數值
print(f"Your BMI number is {the_number}")
最終輸出為這樣
Your BMI number is 20.351562499999996
浮點數在遇到無限小數後,會進行取捨。
用戶輸入的值也無法直接進行計算,而是需要轉化為浮點數才能進行計算,很巧的是,用戶輸入的值也正是整數和字串,因此也可以使用 float ()
函數進行轉化
舉例子 #3
#輸入值
print("\nEnter your weight(kg)")
weight = input()
print("\nEnter your height(m)")
height = input()
#轉化值
your_weight = float(weight)
your_height = float(height)
#計算
number = your_weight/(your_height*your_height)
the_number = float(number)
#得出數值
print(f"\nYour BMI number is {the_number}")
在運行後是這樣的:
Enter your weight(kg)
51.2
Enter your height(m)
1.6
Your BMI number is 20.351562499999996
if-elif-else 語句#
我們需要檢查兩個甚至更多不同的情況,為此我們可以使用 if-elif-else
結構。它依次檢查每個條件測試,直到遇到通過了的條件測試。測試通過後,將會執行後面的代碼,並且跳過餘下的 if-elif-else
語句下的代碼,為了更方便解釋,直接舉例子將會勝過寫用法。
舉例子 #1
我們需要的要求:
BMI 值小於 18.5 的,告訴他低於了標準值
BMI 值大於或等於 18.5 且小於或等於 23.9 的,告訴他達到了標準值
BMI 值大於 23.9 但小於或等於 27.9 的,告訴他超過了標準值
通過數學中,區間的方式,我們知道,這些值的區間為 (-∞,27.9]
在這些值外的所有值,都是嚴重超過了標準,因此我們要告訴他這嚴重超過了標準值。
基於這些需求我們需要的 if-elif-else
語句就長這樣:
#分析
if the_number < 18.5:
print(f"You're underweight and BMI is below standerd.")
elif 18.5 <= the_number <= 23.9:
print(f"Your BMI is normal.")
elif 23.9 < the_number <= 27.9:
print(f"You're overweight and BMI is higher than the standard.")
elif 27.9 < the_number <= 32:
print(f"You're too fat and BMI well above the mark")
else:
print(f"You're severely exeed the limit please lose weight")
前面的 if-elif
包含了 (-∞,27.9]
這個區間內所有的數,因此 else
僅包含了 (27.9,+∞)
也就是嚴重超過了標準值的部分。
接下來讓我們假設值為 20
the_number = 20
if the_number < 18.5:
print(f"You're underweight and BMI is below standerd.")
elif 18.5 <= the_number <= 23.9:
print(f"Your BMI is normal.")
elif 23.9 < the_number <= 27.9:
print(f"You're overweight and BMI is higher than the standard.")
elif 27.9 < the_number <= 32:
print(f"You're too fat and BMI well above the mark")
else:
print(f"You're severely exeed the limit please lose weight")
那麼我們得到的將會是如下內容:
Your BMI is normal.
由於 if-elif-else
語句和 Python 的特性,使得該部分內容,與自然語言十分接近,因此翻譯的方法將會十分適用,具體的翻譯便不再展示。
尾聲#
在一年前,我和許多朋友們一起討論過那些大神們的,他們用寥寥幾行代碼,就可以完成別人幾百行的任務,是十分強大的,大家也要有足夠的積澱才能成為那種大神。我不會否認這種人的存在,也不會否認他們的強大,但是就我的認知來說,真正好的編程能力,是能夠清晰地展現自己所寫的內容,能清晰、簡潔地告訴大家這部分是做什麼的,那部分又是做什麼,而不是一筆完成,這樣才有編程的意義。當然適當的縮短也可以更好的表示自己的意思,就如同自然語言 - 漢語中的成語一樣,這期的名稱之所以叫面向未來的 BMI 計算,也正是因為這個,未來的編程中,也只會是清晰的代碼,為此我在分析代碼時,全程進行都是規範、清晰地書寫,這是為了,面向未來!
此文由 Mix Space 同步更新至 xLog
原始鏈接為 https://fmcf.cc/posts/technology/Python_Notes_3