"out of range" else clause causes syntax error

I want to check the value range of an input number, then print the correct "size" (small, medium or large). If the value is out of my acceptable range, then I want the else statement to print out that the number is not valid.

Minimal example for my problem:

n = int(input("number= "))
if 0 <= n < 5:
    a = "small"
if 5 <= n < 10:
    a = "medium"
if 10 <= n <= 20:
    a = "large"
print("this number is",a)
else:
    print("thats not a number from 0 to 20")

According to Google, this is a problem with indentation. I've tried multiple ways of indenting this; I can fix the syntax, but I can't get the logic correct.

Let's fix your immediate issue: you have an else with no corresponding if statement. Syntactically, this is because you have an intervening "out-dented" statement, the print, which terminates your series of ifs.

Logically, this is because you have two levels of decision: "Is this a number 0-20?", and "Within that range, how big is it?" The problem stems from writing only one level of ifs to make this decision. To keep close to your intended logic flow, write a general if on the outside, and encapsulate your small/medium/large decision and print within that branch; in the other branch, insert your "none of the above" statement:

n = int(input("number= "))
if 0 <= n <= 20:
    if n < 5:
        a = "small"
    elif n < 10:
        a = "medium"
    else:
        a = "large"
    print("this number is", a)    
else:
    print("that's not a number from 0 to 20")

You should try something like

n = int(input("number= "))
if 0 <= n < 5:
    a = "small"
elif 5 <= n < 10:
    a = "medium"
elif 10 <= n <= 20:
    a = "large"
else:
    a = "not a number from 0 to 20"

print("this number is",a)

The print statement before the else statement needs to either be removed or indented to match:

a= "large"

You've syntax (indentation) error:

n = int(input("number= "))
if 0 <= n < 5:
    a = "small"
if 5 <= n < 10:
    a = "medium"
if 10 <= n <= 20:
    a = "large"
#print("this number is",a) indentation error in this line
else:
    print("thats not a number from 0 to 20")

You can use also use following code

n = int(input("number= "))

if 10 <= n <= 20:
    a = "large"
    print("this number is",a)
elif 5 <= n < 10:
    a = "medium"
    print("this number is",a)
elif 0 <= n < 5:
    a = "small"
    print("this number is",a)
else:
    print("thats not a number from 0 to 20")

Comments
  • you have a print statement between your last if and the else. remove that and you'll be fine
  • else needs to be followed by an if. It cannot be by itself. You can move the print("this number is",a) to be after your else.
  • @SpencerWieczorek Other way around. else needs to follow an if or elif
  • @PatrickHaugh My bad, that's what I mean't.
  • This is the answer that worked for me. Others tried to fix the indentation in the code but like I said that didn't really fix the issue, it just removed the error message.
  • Thanks for the code edits. This isn't my best focus day.
  • This works! Unfortunately in my actual code I cannot assign the variable "a" to the else function. I did find help with a workaround though.
  • what do you mean I cannot assign the variable "a" to the else function? the posted code does exactly what you're asking for; when I input 22, I get the output: ('this number is', 'not a number from 0 to 20')
  • What I mean is the variable "a" wasn't originally under the else function. For this particular problem I created for the sake of understanding, your workaround is valid, but in my actual code that I have, I cannot assign the variable because it wouldn't make out a coherent sentence like it does here. For instance in this problem if I wanted the else statement to say "Can you not count to 20?"