Ask coding questions

← Back to all posts
I need help
FARHEENPALAGIRI (9)

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??

Comments
hotnewtop
CodeLongAndPros (1589)

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")
safan41 (42)

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:
     ...

)