+ 1

can anybody explain the code?

m=0 x=1 while x<5: y=1 while y<4: m=m+y y=y+3 x=x+2 print (m) >>>2 Can anybody explain why out put is 2?

1st Nov 2020, 8:14 AM
shahrbanoogolmohamadi
2 Réponses
+ 3
x=1 x<5 y=1 y<4 m=1 y=4 x=3 x<5 y=1 y<4 m=2 y=4 x=5
1st Nov 2020, 8:20 AM
Abhay
Abhay - avatar
+ 3
To see how it happens, insert print() functions inside the loops, e.g. m = 0 x = 1 while x < 5: print("x1", x) y = 1 while y < 4: print("y1", y) print("m1", m) m = m + y print("m2", m) y = y + 3 print("y2", y) x = x + 2 print("x2", x) print(m)
1st Nov 2020, 8:31 AM
David Ashton
David Ashton - avatar