-1

I'm still in the early stages of learning python so this is probably an easy question that I just can't find the answer for. I'm trying to take user input to define 2 variables and compare them using > and < in an if statement.

line 6-11 in the code below I've also tried ...is False: and also y > x is True

print("what is x")
x = int(input('> ))
print("what is y")
y = int(input('> ))

if x > y is True:
    print("x > y")
elif x > y is not True:
    print("y > x")
else:
    print("whatever")      

If x > y then it says y > x. If y > x, it prints the else condition.

Ryan
  • 23
  • 4

1 Answers1

0

You need brackets around your x/y comparison. I modified the code and I think it works as you intended now:

print("what is x")
x = int(input('> '))
print("what is y")
y = int(input('> '))
if (x > y) is True:
    print("x > y")
elif (x > y) is not True:
    print("y > x")
else:
    print("whatever")

EDIT: As others have pointed out in the comments, you don't have to explicitly compare the result of x > y. You can just do this:

print("what is x")
x = int(input('> '))
print("what is y")
y = int(input('> '))
if x > y
    print("x > y")
elif y > x:
    print("y > x")
else:
    print("whatever")
Owen
  • 404
  • 5
  • 13
  • I knew it was something dumb like that. It works now, thanks! – Ryan Jan 31 '19 at 21:59
  • 1
    The parens in `if (x > y)` and are useless in your second example. –  Jan 31 '19 at 22:02
  • You're right @Jean-ClaudeArbaut. I've removed them from the example. They were just left over from copy-pasting the code from above. – Owen Jan 31 '19 at 22:05