Python 스크립팅 기초 - BMI 계산기
내용물
Code
요약
In today's post I will walk you step by step on how to write a basic python script to calculate your body mass index(B.M.I). We will ask the user for some input, and based on it, calculate their B.M.I and return also tell the user, which category their B.M.I falls under. Body mass index is defined by the formula as below:
B.M.I = [your weight in (Kg) / (your height in (m))^2]
Kg - Kilogram
m - metre
There are several defined B.M.I categories, such as:
Underweight = B.M.I below < 18.5.
Normal weight = B.M.I between 18.5-24.9.
Overweight = B.M.I between 25-29.9.
Obesity = B.M.I 30 or above 30.
암호
사용자로부터 데이터 가져오기
>>> user_name = input("Enter your name: ")
Enter your name: Soumyajyoti Biswas
>>> print(user_name)
Soumyajyoti Biswas
>>>
user_name = input("Enter your name: ")
user_weight = float(input("Enter your weight in Kg(s): "))
user_height = float(input("Enter your height in cm(s): "))
B.M.I 계산하기
- Lets take the data that the user provided and put it through our B.M.I formula. Note that the user is providing their height in cm(s). So we will convert that to metre first. To convert cm to m, you divide it by 100.
>>> user_name = input("Enter your name: ")
Enter your name: Soumyajyoti Biswas
>>> user_weight = float(input("Enter your weight in Kg(s): "))
Enter your weight in Kg(s): 70
>>> user_height = float(input("Enter your height in cm(s): "))
Enter your height in cm(s): 165
>>> bmi = round(user_weight/((user_height/100) ** 2),1)
>>> print(bmi)
25.7
# Error displayed if you try to cast a string to a float. [Ref1]
>>> x = input()
12345
>>> type(x)
<class 'str'>
>>> x + x
'1234512345'
>>> type(x + x)
<class 'str'>
>>> x / 10
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for /: 'str' and 'int'
계산된 B.M.I를 기반으로 B.M.I 카테고리 얻기
>>> if bmi < 18.5:
... result = "Underweight"
... elif bmi >= 18.5 and bmi <= 24.9:
... result = "Normal"
... elif bmi >= 25 and bmi <= 29.9:
... result = "Overweight"
... else:
... result = "Very overweight"
...
>>> print(result)
Overweight
함께 모아서
- Let's put all the code together to build a script
user_name = input("Enter your name: ")
user_weight = float(input("Enter your weight in Kg(s): "))
user_height = float(input("Enter your height in cm(s): "))
bmi = round(user_weight/((user_height/100) ** 2),1)
if bmi < 18.5:
result = "Underweight"
elif bmi >= 18.5 and bmi <= 24.9:
result = "Normal"
elif bmi >= 25 and bmi <= 29.9:
result = "Overweight"
else:
result = "Very overweight"
print(f"Hello {user_name}. Your BMI is {bmi}. Your body mass index is {result}.")
Reference
이 문제에 관하여(Python 스크립팅 기초 - BMI 계산기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/soumyajyotibiswas/python-scripting-basics-bmi-calculator-3eee텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)