-3

So I am trying to calculate BMI which allows user to choose the metric or imperial system. For example if user input metric 1.8 78. The program will calculate base on the system chosen either imperial or metric. However after inputting the formula and also doing the if else statement the output is not showing the result. Need some advice

import sys
                len(sys.argv) == 4
    #ensure correct str and float is inputted
                try:
                    units = (sys.argv[1])
                    weight = float(sys.argv[2])
                    height = float(sys.argv[3])
                except (ValueError, IndexError):
                    print("Your input is invalid!")
            #to ensure only 'metric' or 'imperial' is inputted
                while True:
                    units = (sys.argv[1])
                    if units == ('metric' or 'imperial'):
                        break
                    else:
                        print("Your input is invalid")
    #calculations for the bmi
                metric_bmi = weight / (height ** 2)
                us_bmi = 703*weight/ (height ** 2)
    #to check for which system to calculate and the result of the bmi to give the output
                if units == 'metric' and metric_bmi == 16:
                    print("BMI:%.2f" % metric_bmi + "\tSevere Thinness")
                elif units == 'metric' and 16 == metric_bmi <17:
                    print("BMI:%.2f" % metric_bmi + "\tModerate Thinness")
                elif units == 'metric' and 17 == metric_bmi < 18.5:
                    print("BMI:%.2f" % metric_bmi + "\tMild Thinness")
                elif units == 'metric' and 18.5 == metric_bmi < 25:
                    print("BMI:%.2f" % metric_bmi + "\tNormal")
                elif units == 'metric' and 25 == metric_bmi < 30:
                    print("BMI:%.2f" % metric_bmi + "\tOverweight")
                elif units == 'metric' and 30 == metric_bmi < 35:
                    print("BMI:%.2f" % metric_bmi + "\tObese Class I ")
                elif units == 'metric' and 35 == metric_bmi < 40:
                    print("BMI:%.2f" % metric_bmi + "\tObese Class II")
                elif units == 'metric' and  metric_bmi > 40:
                    print("BMI:%.2f" % metric_bmi + "\tObese Class III")
                else:
                print("Your input is invalid!")
                if units == 'imperial' and us_bmi == 16:
                    print("BMI:%.2f" % us_bmi + "\tSevere Thinness")
                elif units == 'imperial' and 16 == us_bmi == 16<17:
                    print("BMI:%.2f" % us_bmi + "\tModerate Thinness")
                elif units == 'imperial' and 17 == us_bmi < 18.5:
                    print("BMI:%.2f" % us_bmi + "\tMild Thinness")
                elif units == 'imperial' and 18.5 == us_bmi < 25:
                    print("BMI:%.2f" % us_bmi + "\tNormal")
                elif units == 'imperial' and 25 == us_bmi < 30:
                    print("BMI:%.2f" % us_bmi + "\tOverweight")
                elif units == 'imperial' and 30 == us_bmi < 35:
                    print("BMI:%.2f" % us_bmi + "\tObese Class I")
                elif units == 'imperial' and 35 == us_bmi < 40:
                    print("BMI:%.2f" % us_bmi + "\tObese Class II")
                elif units == 'imperial' and  us_bmi > 40:
                    print("BMI:%.2f" % us_bmi + "\tObese Class III")
                else:
                    print("Your input is invalid!")
theslasher
  • 21
  • 5
  • 1
    Sorry there was some problem trying to include my code in the body just now – theslasher Sep 10 '22 at 15:52
  • 2
    Change `if units == ('metric' or 'imperial'):` to `if units == 'metric' or units == 'imperial':`, or alternatively, `if units in ('metric', 'imperial'):`. – 9769953 Sep 10 '22 at 15:53
  • see the guide to [formatting](/help/formatting). it's best if you clean up and properly indent your code in an editor and then paste it in. make sure you post an entire working example - while it's important to try to make your code the *minimal* amount necessary to demonstrate the issue, it's also important that it be *complete/reproducible*. – Michael Delgado Sep 10 '22 at 15:54
  • 1
    what is this comparison supposed to do? `if ... and (25 == metric_bmi < 30):` - do you mean `25 <= metric_bmi < 30`? – Michael Delgado Sep 10 '22 at 15:55
  • also, was this intended to be an `assert` statement? `assert len(sys.argv) == 4`? – Michael Delgado Sep 10 '22 at 15:56
  • Hi I'm new to python whats the difference between if units == ('metric' or 'imperial'): and if units == 'metric' or units == 'imperial': – theslasher Sep 10 '22 at 15:57
  • python always evaluates the expression in the parentheses first. you can try out the expression `("metric" or "imperial")` in an interpreter - the result is simply `"metric"` so the expression `if units == ('metric' or 'imperial'):` reduces to `if units == "metric":`. see this great explanation of the behavior of `and` and `or` with non-boolean values: https://stackoverflow.com/questions/47007680/how-do-and-and-or-act-with-non-boolean-values. on the other hand, `if units in ("metric", "imerial")` checks to see whether `units` matches one of the elements of the tuple. – Michael Delgado Sep 10 '22 at 15:58
  • curious though - are you always getting "your input is invalid" or do you just get nothing? if the latter, it would be great to see all of your code, not just the function body or whatever you posted here. – Michael Delgado Sep 10 '22 at 16:03
  • I'm just getting "Your input is invalid". This is all of my code – theslasher Sep 10 '22 at 16:06

1 Answers1

0
import sys
if len(sys.argv) == 4:
    #ensure correct str and float is inputted
    try:
        units = str(sys.argv[1])
        weight = float(sys.argv[2])
        height = float(sys.argv[3])
    except:
        raise ValueError("Your input is invalid!")
        #to ensure only 'metric' or 'imperial' is inputted
    if not units in ('metric', 'imperial'):
        raise ValueError("Your unit type input is invalid")
    #calculations for the bmi
    metric_bmi = weight / (height ** 2)
    us_bmi = 703*weight/ (height ** 2)
    #to check for which system to calculate and the result of the bmi to give the output
    if units == 'metric':
        if metric_bmi < 16:
            print("BMI:%.2f" % metric_bmi + "\tSevere Thinness")
        elif metric_bmi < 17:
            print("BMI:%.2f" % metric_bmi + "\tModerate Thinness")
        elif metric_bmi < 18.5:
            print("BMI:%.2f" % metric_bmi + "\tMild Thinness")
        elif metric_bmi < 25:
            print("BMI:%.2f" % metric_bmi + "\tNormal")
        elif metric_bmi < 30:
            print("BMI:%.2f" % metric_bmi + "\tOverweight")
        elif metric_bmi < 35:
            print("BMI:%.2f" % metric_bmi + "\tObese Class I ")
        elif metric_bmi < 40:
            print("BMI:%.2f" % metric_bmi + "\tObese Class II")
        else:
            print("BMI:%.2f" % metric_bmi + "\tObese Class III")
    else:
        if us_bmi < 16:
            print("BMI:%.2f" % us_bmi + "\tSevere Thinness")
        elif us_bmi < 17:
            print("BMI:%.2f" % us_bmi + "\tModerate Thinness")
        elif us_bmi < 18.5:
            print("BMI:%.2f" % us_bmi + "\tMild Thinness")
        elif us_bmi < 25:
            print("BMI:%.2f" % us_bmi + "\tNormal")
        elif us_bmi < 30:
            print("BMI:%.2f" % us_bmi + "\tOverweight")
        elif us_bmi < 35:
            print("BMI:%.2f" % us_bmi + "\tObese Class I")
        elif us_bmi < 40:
            print("BMI:%.2f" % us_bmi + "\tObese Class II")
        else:
            print("BMI:%.2f" % us_bmi + "\tObese Class III")
else:
    print("You didn't enter the correct number of arguments")
Alex
  • 707
  • 1
  • 4
  • 9
  • Hi I have a question when I input metric 1.80 78 the output should be 24.07 and normal but the output I'm getting is 0.00 and severe thinness – theslasher Sep 10 '22 at 16:34
  • your script assumes that you need to first enter the weight and then the height, so your input is expected to be `python3 yourscript.py metric 78 1.8`. I hope this helps. – Alex Sep 10 '22 at 16:41