pythonif-statement

If statement does something weird


Can someone please explain why it doesn't print "Hi", but instead prints "bye"?

a = 5
b = -5

if b > 0 != a > 0:
  print("Hi")
else:
  print("Bye")

Changing it from "!=" to "and not" doesn't help.


Solution

  • You have to add brackets. Currently you're comparing 0 != a.

    Fix:

    a = 5
    b = -5
    
    if (b > 0) != (a > 0):
      print("Hi")
    else:
      print("Bye")