0

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.")
1
  • 1
    Most likely sample_number is the string 4 and not the number 4. Try like this: int(sample_number). Commented Aug 16, 2022 at 22:25

2 Answers 2

1

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.")
Sign up to request clarification or add additional context in comments.

Comments

0

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.")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.