What Should I Do with "Unexpected Indent" in Python

unexpected indent error in Python 3

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()

Python 3 : Unexpected indent in 'if'

if age < 18) should be if (int(age) < 18)

Unexpected indent in Python function

You are mixing tabs and spaces

before if you have used one spaces and two tabs

In python you should not be mixing tabs and space you should use either tab or space

You can find that using python -tt script.py

Most of the python developer prefer space to tab

Can't write new lines because of IndentationError: unexpected indent Python

You have bad indentation, delete blank space before this line and then use TAB to correct indentation, try it :)

IndentationError: unexpected indent - Python 2.7

You used a tabulation character instead of spaces in the last line of your code (it can be seen when editing your post). Juste replace it with four spaces and you won't get this error message any more!



Related Topics



Leave a reply



Submit