Python 스크립팅 기초 - BMI 계산기

내용물


  • Summary

  • Code
  • Taking data from the user
  • Calculating the B.M.I
  • Getting the B.M.I category
  • Putting the code together


  • 요약

    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.
    

    암호

    사용자로부터 데이터 가져오기

  • We are going to use the input 함수, 그리고 사용자가 제공한 데이터를 a 변수에 저장



  • >>> user_name = input("Enter your name: ")
    Enter your name: Soumyajyoti Biswas
    >>> print(user_name)
    Soumyajyoti Biswas
    >>> 
    


  • 우리는 사용자의 이름, Kg(s) 단위의 체중, cm(s) 단위의 키를 사용자에게 물어볼 것입니다.

  • 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
    
  • In the above code I did two things:
  • [1] Convert the user input for weight and height from string to a float data type. The input function provides data output as type string. Hence we have to convert it to a float data type to perform numerical operation on it. See what error comes if you do not [Ref1]. You can see the various datatypes here .
  • [2] B.M.I 계산 결과를 소수점 이하 자릿수로 반올림합니다. round function 작동 방식을 확인하십시오.


  • # 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 카테고리 얻기

  • Let us take the calculated B.M.I and try and place it in a category as defined above. We will use the If / elif / else 하시면 됩니다.

  • >>> 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}.")
    
  • You can download the file from my GitHub 페이지.
  • 좋은 웹페이지 즐겨찾기