Jay Klope
Jay Klope

Reputation: 145

How to determine the output values given multiple "if" statements

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)

Upvotes: 1

Views: 45

Answers (2)

Taohidul Islam
Taohidul Islam

Reputation: 5414

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

Upvotes: 0

ravi
ravi

Reputation: 10733

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

Upvotes: 1

Related Questions