1
# Welcoming message
print('Welcome to the bmi calculator')
user = str(input("What's your name?:"))
print('\n')
# Declaring unit as empty
unit = ''
while not unit:
    unit = str(input("Chose 1 for imperial and 2 for metric units(1/2):"))
    if unit in ("12"):
        if unit == "1":
            print('You have chosen imperial units')
            heighti = float(input('Please enter your height in inches:'))
            weighti = float(input('Now please enter your weight in lbs:'))
            bmi = ((weighti / (heighti**2) * 703))
        elif unit == "2":
            print('You have chosen metric units')
            heightm = float(input('Please enter your height in metres:'))
            weightm = float(input('Please enter your weight in kg:'))
            bmi = (weightm / (heightm**2))
        else:
            unit = ''

        print(f'Your BMI is {bmi}')
        print('\n')
        print('your bmi results show that you are:')
        if bmi > 24:
            print('Overweight')
        elif bmi < 19:
            print('Underweight')
        else:
            print('In The ideal range for human')
    else:
        pass
else:
    print('Sorry,invalid choice,must be either 1 for imperial or 2 for metric')


Project runs smoothly once 1 or 2 is typed by the user, however if an input like 3 is put, the error is:

Sorry,invalid choice,must be either 1 for imperial or 2 for metric

***Repl Closed***

I have edited the code such that the 3rd if statement encapsulates the rest of the code. My situation is that the program works well if 1 or 2 is inputted by the user, however does not start over to ask for user's input if the first input is not 1 or 2.

trinity
  • 33
  • 5

1 Answers1

0

Your IF statement if unit in ("12"): needs an additional else statement. It will accept 1 or 2 but without an else case to catch numbers outside of that scope, you're code wont move to the nested if statements.

ie:

while not unit:
    unit = str(input("Chose 1 for imperial and 2 for metric units(1/2):"))
    if unit in ("12"):
        if unit == "1":
            print('You have chosen imperial units')
            heighti = float(input('Please enter your height in inches:'))
            weighti = float(input('Now please enter your weight in lbs:'))
            bmi = ((weighti / (heighti**2) * 703), 5)
        elif unit == "2":
            print('You have chosen metric units')
            heightm = float(input('Please enter your height in metres:'))
            weightm = float(input('Please enter your weight in kg:'))
            bmi = (weightm / (heightm**2), 2)
        else:
            unit = ''
        print(f'Your BMI is {bmi}')
        print('\n')
        print('your bmi results show that you are:')
        if bmi > 24:
                  print('Overweight')
        elif bmi < 19:
                  print('Underweight')
        else:
            print('In The ideal range for human')

   else: 
        print('Please choose either 1 for imperial or 2 for metric')
ImCrimson
  • 146
  • 1
  • 1
  • 10
  • 1
    Okay that seems to allow the statement ('Please choose either 1 for imperial or 2 for metric) to repeat but it proceeds to: Traceback (most recent call last): File "bmi calculator.py", line 27, in print('\n') NameError: name 'bmi' is not defined – trinity Nov 22 '20 at 16:01
  • That's because in python your indentation determines the scope of what code gets run. whether the number is 1,2, or 3, the final 'print(f'Your BMI is {bmi}')' statement is still run. To prevent this youll have to move the scope of the print statement under the nested if statement, give me one moment and I will edit my answer to include this change – ImCrimson Nov 22 '20 at 16:04
  • 1
    Thanks for your input! Will make a note about your comment on nested if statements. Upon making your changes, with the last else statement being in line with if unit in ("12") Running the program Welcome to bmi calculator What's your name?: Name Chose 1 for imperial and 2 for metric units(1/2):3 Please choose either 1 for imperial or 2 for metric ***Repl Closed*** Does not seem to want to repeat :( – trinity Nov 22 '20 at 16:11
  • I apologize, what are you trying to get done? I don't understand your last comment – ImCrimson Nov 22 '20 at 18:25
  • So with the changes I made that you mentioned, when I input a _wrong number like 3_ the program displays the choice `"Please choose either 1 for imperial or 2 for metric"` but then my REPL closes and the program terminates. Even with having the `else: unit ' '`, the Calculator still will not let me start over if the user input is not 1 or 2. – trinity Nov 22 '20 at 19:30