-2

I have to create a BMI program. The weight should be entered as kilograms and the height as cm, then the program should convert the entered values to imperial equivalents and display them and after it should then calculate the BMI and display the result.

So far I have created a BMI calculator where the user can choose between Metric and Imperial. I have created a while true statement for the user to choose only between metric and imperial and when the user puts the height and the weight it will calculate and then display the result. BUT my problem is that I have to convert the metric values to imperial and I don't know how to do it. I miss the converter for metric to imperial and vice versa, any ideas how to improve it?

while True:
    try:
        if choice == 'Metric':
         weight = float(input('weight in kg:'))
         height = float(input('height in cm:'))

        if choice == 'Imperial':
         weight = float(input('weight in pounds:'))
         height = float(input('height in inches:'))
    except ValueError:
        print('Invalid input')
    else:
        break

if choice == "Metric":
    bmi = weight / (height * height)
    return bmi

if choice == "Imperial":
    bmi = (weight * 703) / (height * height)
    return bmi
2
  • What does this program do wrong? Aug 8, 2020 at 19:26
  • I am trying to create a BMI calculator where the user can choose between Metric and Imperial. I have created a while true statement for the user to choose only between metric and imperial and when the user puts the height and the weight it will calculate and then display the result. BUT my problem is that I have to convert the metric values to imperial and I don't know how to do it. I miss the converter for metric to imperial and vice versa, any ideas? Aug 8, 2020 at 19:43

1 Answer 1

0

You first have to define the variable choice because so far you have created if statements that are just not being activated. So try this:

def conversion_to_metric(weight, height):
    """Convert values from Imperial to Metric"""
    pound = 0.453592
    kg_ans = float(weight * pound)

    inch = 2.54
    cm_ans = float(inch * height)
    print("--------------------")
    print(str(weight) + "lbs. " + "= " + str(kg_ans) + "kg.")
    print(str(height) + "in. " + "= " + str(cm_ans) + "cm.")


def conversion_to_imperial(weight, height):
   """Convert values from Metric to Imperial"""
   kg = 2.20462
   lbs_ans = float(weight * kg)

   cm = 0.393701
   inch_ans = float(cm * height)
   print("--------------------")
   print(str(weight) + "kg. " + "= " + str(lbs_ans) + "lbs.")
   print(str(height) + "cm. " + "= " + str(inch_ans) + "in.")


while True:
    try:
        print("A. Imperial -> Metric\n" + "B. Metric -> Imperial")
        choice = input("Choose a conversion: ")
        if choice == 'B':
            weight = float(input('\tweight in kg:'))
            height = float(input('\theight in cm:'))
            conversion_to_imperial(weight, height)
         
            if weight == 'q' or height == 'q':
                break

        elif choice == 'A':
            weight = float(input('\tweight in pounds:'))
            height = float(input('\theight in inches:'))
            conversion_to_metric(weight, height)
    
            if weight == 'q' or height == 'q':
                break
        
        elif choice == 'q':
            break
         
    except ValueError:
        print('Please enter either A or B...')

Hope this helps. Remember to always give the user an option to quit, or else you will have an endless loop that eats up your memory. Fix a few of my variable around.

1
  • Sorry for some mistakes, I went back and fixed them, so try it now.
    – Maxim
    Aug 8, 2020 at 20:12

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.