Home > OS >  Check input in if else statement not working in python
Check input in if else statement not working in python

Time:08-17

I am wondering what I'm doing wrong. I want the following if/else statement to print "The number is 4." when 4 is entered in the input. But after entering 4, it always says "Number is not 4." It is in python. Thanks in advance


if sample_number == 4:
    print("The number is 4")
else:
    print("Number is not 4.")

CodePudding user response:

The input type is a string so 4 != "4"

This will work:

if sample_number == "4":
    print("The number is 4")
else:
    print("Number is not 4.")

CodePudding user response:

I dont have a lot of context but it's most likely a string so just try converting it like this:

sample_number = int(sample_number)
if sample_number == 4:
    print("The number is 4")
else:
    print("Number is not 4.")
  • Related