day 2-5 python (BMI example)
2023. 2. 21. 15:17ㆍ개인적인 공부/Python
Instructions
Write a program that calculates the Body Mass Index (BMI) from a user's weight and height.
The BMI is a measure of someone's weight taking into account their height. e.g. If a tall person and a short person both weigh the same amount, the short person is usually more overweight.
The BMI is calculated by dividing a person's weight (in kg) by the square of their height (in m):
Warning you should convert the result to a whole number.
Example Input
weight = 80
height = 1.75
Example Output
80 ÷ (1.75 x 1.75) = 26.122448979591837
26
e.g. When you hit run, this is what should happen:
결과값을 정수형으로 내라
# 기본조건
# 🚨 Don't change the code below 👇
height = input("enter your height in m: ")
weight = input("enter your weight in kg: ")
# 🚨 Don't change the code above 👆
#Write your code below this line 👇
내 코드
new_weight = float(weight)
new_height = float(height)
BMI = new_weight/(new_height**2)
result = int(BMI)
print(result)
기본적으로 height 나 weight 나 class str 문자열 이기 때문에 숫자로 바꾸어야 하는데 입력값이 cm 가 아니라 m 이므로 소수점이 들어가기 때문에 float 으로 새로 데이터 타입을 변형해주고 BMI 식을 그대로 대입한다.
결과는 정수형을 원하기 때문에 다시 int 로변경해주어 result 를 출력하도록 하였다.
'개인적인 공부 > Python' 카테고리의 다른 글
day 2-7 python (Life in Weeks Exercise) (0) | 2023.02.21 |
---|---|
day2-6 python (숫자 처리 및 F-String) (0) | 2023.02.21 |
day 2-4 python (수학 연산) (0) | 2023.02.21 |
day 2-3 python (data tpyes exercise) (0) | 2023.02.20 |
day 2-2 python (형식 오류와 형식 확인, 그리고 형 변환) (0) | 2023.02.20 |