-2

I am trying to create a program that will calculate the BMI of a person as my homework. I am getting "unexpected indent" error in line 9 which is ***print ("Your BMI is: ",BMI). I have moved the indent back and forth to correct it but still not working. Please help as I am only a beginner. Thank you in advance.

user_input = float(input("Let's calculate your BMI! Please select if you 'K' for kilograms and L for pounds for your weight: ")).upper


if user_input == "K":
    user_input_weight_kgs = float(input("Weight in kgs.:"))
    user_input_height = float(input("Height by inches: "))
    user_input_age = (input("Age:"))
    BMI_for_kgs  = float(user_input_weight / (user_input_height **2))
        print ("Your BMI is: ",BMI)
    if BMI < 18.5 :
        print ("Under Weight")
    elif BMI < 26:
        print ("Normal Weight")
    else:
        print ("Over Weight")

elif user_input == "L":
    user_input_weight_lbs = float(input("Weight in lbs.: "))  
    user_input_height = float(input("Height by inches: "))
    user_input_age = (input("Age:"))
    BMI_for_lbs  = float((user_input_weight * 703) / (user_input_height **2))
        print ("Your BMI is: ",BMI)
    if BMI < 18.5 :
        print ("Under Weight")
    elif BMI < 26:
        print ("Normal Weight")
    else:
        print ("Over Weight")       

elif guess.isnumeric():
    print ("Please select an alphabet only! Letter 'K' or 'L'")
elif len(guess) > 1:
    print ("Please choose a single alphabet only! Letter 'K' or 'L'")
elif len(guess) == 0:
    print ("You need to enter a letter! Letter 'K' or 'L'")
else:
    break
Iguananaut
  • 21,810
  • 5
  • 50
  • 63
Jonathan
  • 162
  • 1
  • 11
  • 4
    because this line is not indented properly - take it one tab back! – Nir Alfasi Nov 02 '17 at 00:41
  • 2
    your code wouldn't even check the first `if user_input == "K"` because you are converting the input to a `float` and you're calling the `.upper()` method on it. – kstullich Nov 02 '17 at 00:43
  • 1
    Both of those `print ("Your BMI is: ",BMI)` lines are indented too much. They should line up with the lines above them. – PM 2Ring Nov 02 '17 at 00:44
  • aha!! thanks guys I've moved the one tab back and it worked! one prob now is as @kstullich just commented, after entering K, i got an error saying could not convert string to float : 'k'. But should'nt it not after getting K, it should ask the user to enter their weight by kgs? – Jonathan Nov 02 '17 at 00:50
  • 1
    Just use this `user_input = input("Let's calculate your BMI! Please select if you 'K' for kilograms and L for pounds for your weight: ").upper()`. You only want the letter "K" or "L", so you cannot convert a letter to a number. – kstullich Nov 02 '17 at 00:55
  • 1
    Indentation is important in Python. You'll get the hang of it. In the future pay close attention to the error message at the bottom of the "Traceback". It will usually point you right to the line where you have the mistake or at least one near it. – Iguananaut Nov 02 '17 at 00:55
  • @kstullich ha! i get it now! lol yea letter will not get converted to float, issue was in the very beginning (line 1) thanks you so much! – Jonathan Nov 02 '17 at 01:12

1 Answers1

2

The issue is that the line 9 (print line) is not indented properly, here is the fix:

if user_input == "K":
    user_input_weight_kgs = float(input("Weight in kgs.:"))
    user_input_height = float(input("Height by inches: "))
    user_input_age = (input("Age:"))
    BMI_for_kgs  = float(user_input_weight / (user_input_height **2))
    print ("Your BMI is: ",BMI)
    if BMI < 18.5 :
        print ("Under Weight")
    elif BMI < 26:
        print ("Normal Weight")
    else:
        print ("Over Weight")

elif user_input == "L":
    user_input_weight_lbs = float(input("Weight in lbs.: "))  
    user_input_height = float(input("Height by inches: "))
    user_input_age = (input("Age:"))
    BMI_for_lbs  = float((user_input_weight * 703) / (user_input_height **2))
    print ("Your BMI is: ",BMI)
    if BMI < 18.5 :
        print ("Under Weight")
    elif BMI < 26:
        print ("Normal Weight")
    else:
        print ("Over Weight")       

elif guess.isnumeric():
    print ("Please select an alphabet only! Letter 'K' or 'L'")
elif len(guess) > 1:
    print ("Please choose a single alphabet only! Letter 'K' or 'L'")
elif len(guess) == 0:
    print ("You need to enter a letter! Letter 'K' or 'L'")
else:
    break

You should only indent code inside a code block. Indenting starts a block and unindenting ends it. There are no explicit braces, brackets, or keywords that end a block of code in Python.

EDIT

to answer the additional question (in the comments):

Your problem is that you are expecting a string either "K" or "L" it seems but you are trying to convert that string to a float. That should indicate that something is wrong. Additionally, you are not actually calling upper() which is another problem (unless your intent is to call user_input() to get the uppercase value of your string, which I think is not necessary here). To solve:

user_input = input("Let's calculate your BMI! Please select if you 'K' for kilograms and L for pounds for your weight: ").upper()
Cyzanfar
  • 6,997
  • 9
  • 43
  • 81
  • aha!! thanks I've moved the one tab back and it worked! one prob now is as @kstullich just commented, after entering K, i got an error saying could not convert string to float : 'k'. But should'nt it not after getting K, it should ask the user to enter their weight by kgs? – Jonathan Nov 02 '17 at 00:53
  • 1
    What @kstullich means is that `user_input == "K"` will always be false because you're comparing a float with a string. Also on your first line you are converting a string to a float. That will only work if your string represents a number (ei: `"1.5"`, `"22"`, etc...). If the only valid options are "K" and "L" then don't convert the input to a float just ensure that it's uppercase and you should be good to go – Cyzanfar Nov 02 '17 at 00:57
  • ok i'll update my answer so as to include this additional question you have – Cyzanfar Nov 02 '17 at 01:10
  • ha! i get it now! lol yea letter will not get converted to float, issue was in the beginning thanks you! – Jonathan Nov 02 '17 at 01:11