학습목표: BMI 계산기를 만들어보자굿

참고자료: BMI계산이란(위키피디아) / Repl.it_BMI계산기

  1. 체질량지수(BMI)를 구하는 코드를 짜보자
  • 체질량 지수는 키와 무관하게 사람의 신체구성을 측정하는 방법임.

  • 키, 몸무게 기준으로 저체중/표준체중/과체중임을 판별한다

Instructions

Write a program that calculates the Body Mass Index (BMI) from a user's weight and height.

The BMI is a measure of some'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):

Example Input

weight = 80
height = 1.75

Try it!

1. 오답

  • print(type(weight)) 하면 <class 'string'> 이므로 정수처리는 잘 해줬음

    입력함수로 부터 값을 입력받았으므로 기본형태는 string이기 때문임.

# 🚨 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 👆
Bmi = int(weight / height**2)
print(Bmi)

# 실행결과: 오류! int() 함수 안의 ** / 연산을 정수로 인식불가능해서 오류 발생함

2. 오답

# 🚨 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 👆

Bmi = int(weight) / int(height) ** 2
print(Bmi)

# 실행결과: 0.00252173585...  오답!

3. 정답

  • 본 코드에서 변수로 키, 무게를 받았으므로 바로바로 쓸 수 있음

  • 몸무게는 정수형이든 실수형이든 상관없는데, 키는 1.0~2.0 m 사이가 많으므로 키는 부동소수형태여야 함

  • 몸무게 정수형으로 명시하면, 몸무게 입력을 소수점으로 입력했을때 오류나니까 float() 형태로 해주는게 맘편할듯?

height = input("enter your height in m: ")
weight = input("enter your weight in kg: ")
Bmi = int(weight) / float(height) ** 2
print(bmi)     #여기까지 실행결과는 소숫점 뒤에 어어어어ㅓ어어엄청 많이 출력됨
                 # int(Bmi) 해서 정수형으로 바꿔줄순 없음. 이미 Bmi에 형변환을 써먹었기 때문이고
                  Bmi = int(int(weight) / float(height) ** 2) 이렇게 표현하면 int형변환을 **수식 /수식에 적용못한다고 지랄남
                  때문에 새 변수를 선언하자 그냥
height = input("enter your height in m: ")
weight = input("enter your weight in kg: ")
Bmi = int(weight) / float(height) ** 2
Bmi_as_int = int(Bmi)
print(Bmi_as_int)
# 실행결과: 
 enter your height in m: 1.63
 enter your weight in kg: 67
 25
  1. 이것도 정답
# 🚨 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 👇

weight_as_int = int(weight)
height_as_float = float(height)

# Using the exponent operator **
bmi = weight_as_int / height_as_float ** 2
# or using multiplication and PEMDAS
bmi = weight_as_int / (height_as_float * height_as_float)

bmi_as_int = int(bmi)

print(bmi_as_int)

#유데미 #유데미코리아 #스타트위드유데미 #스터디윗미

profile
Swift

0개의 댓글