If/else stops after a few statements

Question:

I am trying to create and if/elseif statement so that when user inputs a number it gives him the answer of how many integers he has.

message = int(input("Enter an integer:"))

if message < 0:
    print("1 digit, negative")
elif message < 10:
    print("1 digit")
elif message >= 10:
    print ("2 digits") 
elif message >= 100:
    print("3 digits")
elif message >= 1000:
    print("more than 3 digits")

To my knowledge the if/else statement can be used as many times as you want and it seems like it stops working after 3statement (elif message >= 10:) , but if I temporarily comment out the 3nd statement the 4th statement(elif message >= 100:) works ,but the 5 statements does not.

What am I doing wrong?

Asked By: Dravenzo

||

Answers:

I think you meant this instead:

if message < 0:
    print("negative")
elif message < 10:
    print("1 digit")
elif message < 100:
    print ("2 digits") 
elif message < 1000:
    print("3 digits")
else:
    print("more than 3 digits")

Note how all of these conditions use <.

Answered By: John Gordon
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.