1

First time asking a question on stackoverflow. I am stuck trying to solve this. This is my code:

a = int(input()) 
b = int(input())

Given two non-zero integers, print "YES" if exactly one of them is positive and print "NO" otherwise.

if (a > 0) or (b > 0):
    print('YES') 
else:
    print('NO')
khelwood
  • 55,782
  • 14
  • 81
  • 108
Rich Spence
  • 19
  • 1
  • 3

5 Answers5

3
if (a>0) != (b>0):
    print("YES")
else:
    print("NO")

How do you get the logical xor of two variables in Python?

SIGSTACKFAULT
  • 919
  • 1
  • 12
  • 31
1

You could do this with more complex Boolean operations, but having multiple conditions is the simplest way:

a = int(input())
b = int(input())

if (a > 0 and b < 0) or (a < 0 and b > 0):
    print('YES')
else:
    print('NO')
iz_
  • 15,923
  • 3
  • 25
  • 40
1
print('YES' if a * b < 0 else 'NO')
acushner
  • 9,595
  • 1
  • 34
  • 34
Henk_Jan
  • 11
  • 1
1

Tomothy32's answer is the best approach for sure as-goes simplicity and more importantly, understandability. But here's another way of doing the same thing, just to illustrate how another programmer might do this:

onePositive = ( (a > 0 and b < 0) or (a < 0 and b > 0) )

print('yes' if onePositive else 'no' )
Alex
  • 183
  • 1
  • 10
0

Not the fastest solution or 1-liner but will help you understand my thought process in solving the problem given exactly 2 non zero integers, print yes if exactly one of them is positive and no otherwise.

Solution - you want EXACTLY ONE POSITIVE MEANING THE OTHER ONE MUST BE NEGATIVE if both integers are NON-ZERO

a = int(input())
b = int(input())

#if a is positive and b and negative
if (a > 0) and (b < 0) :
    print('YES')
#if a is negative and b is positive
elif (a < 0) and (b > 0) :
    print('YES')
else :
    print('NO')
SiggiSv
  • 1,219
  • 1
  • 10
  • 20