0

Need substition for using label/goto -used in languages like C,..- in python

So basically, I am a newb at python (and honestly, programming).I have been experimenting with super basic stuff and have hit a "roadblock".

print("Hello User!")
print("Welcome to your BMI Calculator.")
print("Please choose your system of measurement (Answer in terms of A or B):")
print("A)Inches and Pounds")
print("B)Meters and Kilograms")
ans = str(input("Input:A or B"))
if ans =="B":
    h = float(input("Please enter your height (in meters)"))
    w = float(input("Please enter your weight (in kilograms)"))
    bmi = w/(h**2)
    print("Your BMI is:")
    print(bmi)
    if bmi < 18.5:
         print("You are in the UNDERWEIGHT category.")
    elif 18.5 < bmi <24.9:
        print("You are in the HEALTHY-WEIGHT category.")
    else:
        print("You are in the OVERWEIGHT category.")
    print("THANK YOU FOR YOUR TIME.")
elif ans =="A":
    h = float(input("Please enter your height (in inches)"))
    w = float(input("Please enter your weight (in pounds)"))
    bmi = (w*0.453592)/((h*0.0254)**2)
    print("Your BMI is:")
    print(bmi)
    if bmi < 18.5:
        print("You are in the UNDERWEIGHT category.")
    elif 18.5 < bmi <24.9:
        print("You are in the HEALTHY-WEIGHT category.")
    else:
        print("You are in the OVERWEIGHT category.")
    print("THANK YOU FOR YOUR TIME.")
else:
        print("ERROR")

In the final else, I want it to go back to asking for input for the measurement system in case the user did not type 'A' or 'B' exactly. Some substitute for goto in python should work for that. Also, if there isn't a substitute for goto, the problem should be solved if it doesn't take any other input other than 'A' or 'B'. Thanks for your time.

kareem_emad
  • 1,123
  • 1
  • 7
  • 12
SKD
  • 1
  • 2
    You need to use a loop, not a goto. Python doesn't have goto, anyway. And since the days of [structured programming](https://en.wikipedia.org/wiki/Structured_programming) (over 60 years ago!), we _strongly_ avoid using goto. – Óscar López Mar 26 '20 at 18:19
  • put everything in a function, call the function in the else statment. – hansTheFranz Mar 26 '20 at 18:20
  • 1
    @hansTheFranz true, but that would be a recursive function. Not the best idea, when you're just learning how to program. – Óscar López Mar 26 '20 at 18:21
  • What happens if the user enters a height of 0 (zero)? Also, the last 7 lines of each conditional block are identical, so you should move them to a common place. – jarmod Mar 26 '20 at 18:52
  • 1
    There is no equivalent in Python. As stated above, Python was a language that came out after structured programming became a thing (i.e. within the last 60 years...). The flow of a program is controled by 1) Sequence - things move line by line in a sequence), 2) Selection - this is conditional branching, so if...elif...else 3) Iteration / looping - so for-loops and while loops 4) recursion. Here, you probably just want a while loop. – juanpa.arrivillaga Mar 26 '20 at 19:58

2 Answers2

1

I don't recommend you start your programming career with goto, that's how you can get perfectly written spaghetti code.

Let's review your use case here, you want to go back to asking user if he did not gave you an expected input, why not use a loop instead of goto?

ans = "unexpected"
while(ans != "A" and ans != "B"):
  print("Hello User!")
  print("Welcome to your BMI Calculator.")
  print("Please choose your system of measurement (Answer in terms of A or B):")
  print("A)Inches and Pounds")
  print("B)Meters and Kilograms")
  ans = str(input("Input:A or B"))

if ans =="B":
    h = float(input("Please enter your height (in meters)"))
    w = float(input("Please enter your weight (in kilograms)"))
    bmi = w/(h**2)
    print("Your BMI is:")
    print(bmi)
    if bmi < 18.5:
         print("You are in the UNDERWEIGHT category.")
    elif 18.5 < bmi <24.9:
        print("You are in the HEALTHY-WEIGHT category.")
    else:
        print("You are in the OVERWEIGHT category.")
    print("THANK YOU FOR YOUR TIME.")
elif ans =="A":
    h = float(input("Please enter your height (in inches)"))
    w = float(input("Please enter your weight (in pounds)"))
    bmi = (w*0.453592)/((h*0.0254)**2)
    print("Your BMI is:")
    print(bmi)
    if bmi < 18.5:
        print("You are in the UNDERWEIGHT category.")
    elif 18.5 < bmi <24.9:
        print("You are in the HEALTHY-WEIGHT category.")
    else:
        print("You are in the OVERWEIGHT category.")
    print("THANK YOU FOR YOUR TIME.")

With that the user will be kept in the input state as long as he does not give you the desired input format.

kareem_emad
  • 1,123
  • 1
  • 7
  • 12
0

Put everything in 'while (true)' starting with first 'if' statement. And remove last 'else' statement.

gxost
  • 1