← Back to Community
I need help
Profile icon
FARHEENPALAGIRI

Hi everyone. Me again. I need help with a program I'm working on (as usual). So in this code there are two numbers inputted. If exactly one is negative, the program should print YES. If anything else is the input, the program should print NO. So I wrote this:

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

if a < 0:
print("YES")

elif b < 0:
print("YES")

elif a < 0 and b < 0:
print("NO")

else:
print("NO")

The computer seems to be ignoring the command of if both numbers are less than 0. Every time I input two number, the computer outputs YES instead of NO. How do I make it not do that? Help me please??

Voters
Profile icon
FARHEENPALAGIRI
Comments
hotnewtop
Profile icon
CodeLongAndPros

Here's how I would do it:

a = int(input()) b = int(input()) if (abs(a) == a or abs(b) == b) and (abs(b) != b or abs(a) != a): print("YES") else: print("NO")
Profile icon
safan41

your first if statement checks for if a < 0, which input precedes to be true, meaning the elifs after that won't run. try to put the most specific conditional statements first then down to the least specific. (e.g.

if a < 0 and b < 0: ... elif a < 0: ...

)

Profile icon
FARHEENPALAGIRI

@safan41 Thanks :D