0
print("Hello World")
myName = input("What is your name?")
myVar = input("Enter a number:")
if myName == "MZ" or myVar != 0:
    print("MZ is great")
elif myName == "MV":
    print("MV is ok")
else:
    print("Hello World")

No matter what I input as name myName or number myVar, it always prints MZ is great.

Please help. Thanks

2
  • No, probably a string. Because that's what input() returns. And since the comparison is (in)equality, no exception is raised. Commented Apr 28, 2017 at 0:31
  • Parentheses are not necessary on the if statement, by the way Commented Apr 28, 2017 at 0:37

2 Answers 2

4

First off, MZ is great!

This happens because of or myVar != 0. Since input() returns a string (in Python 3) and you compare it to a number, myVar will never be equal to the number 0. You should do something like:

myVar = int(input("Enter a number:"))

You should probably also catch ValueError in case the user types a bad number.

Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou Allen and kichik. Tried both solutions and both work fine. MZ is great! :)
3

Change

if(myName == "MZ" or myVar != 0)

to

if(myName == "MZ" or myVar != "0")

User input is string by default.

1 Comment

Thankyou Allen and kichik. Tried both solutions and both work fine. MZ is great! :)

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.