1

When I input the "a" value as 1 and the "b" value as 1, the answer printed will be 2,-1. Why is it not 3,-1?

a = int(input("a value:"))
b = int(input("b value:"))
if a > 0 and b > 0:
    a = a + 1
    b = b - 1
if a > 0 or b < 0:
    b = b - 1
if b > 0 or a < 0:
    a = a + 1
print(a, b)
2
  • 3
    The third if statement never runs since b=-1 and a=2 Commented Jun 5, 2018 at 3:50
  • @Miket25 Ahh, thank you! That makes sense Commented Jun 5, 2018 at 3:53

2 Answers 2

1

For your input, following will be executed in sequence.

if a > 0 and b > 0:       # (a,b) = (1,1)
    a = a + 1             # a = 2
    b = b - 1             # b = 0
if a > 0 or b < 0:
    b = b - 1             # -1
Sign up to request clarification or add additional context in comments.

Comments

0
a = int(input("a value:"))
b = int(input("b value:"))
if a > 0 and b > 0: #1>0 and 1>0;so True and True=True,that's why this block will be executed
    a = a + 1 #a = a+1=1+1=2
    b = b - 1 #b = b-1 = 1-1=0
if a > 0 or b < 0: #1>0 or 0<0;so True or False=True that's why this block will be executed
    b = b - 1 #b=b-1=0-1=-1
if b > 0 or a < 0: #-1>0 or 2<0; so False or False=False,that's why this block won't be executed
a = a + 1
print(a, b) #2,-1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.