0

Practice nested loop

m = 0 x = 1 while x < 4: y = 1 while y < 5: m = m + y y = y + 1 if x + y == 5: break x = x + 1 print (m) My solution as follows: m=0 x=1 x<4 y=1 1<5 m=0+1=1 y=1+1=2 (x+y=2)false 2<5 m=1 x=1 y=2 y=2 m=1+2=3 y=2+1=3 (x+y=2+1)false 3<5 m=3 y=3 x=2 m=3+3 m=6 x+y=2+3=5(True) Inner loop ends x=x+1 x=1+1=2 2<4 y=1 x=2 m=6+1 m=7 y=y+1 y=1+1 y=2 x+y=2+1=3(False) Stuck with outer loop

29th Jan 2022, 3:24 AM
Knowledge Is Power
Knowledge Is Power - avatar
2 odpowiedzi
0
Knowledge Is Power some of your variable tracking has mistakes. You should go over it more carefully. Where you left off, here is the state they should be, and how it finishes: m=7 x=2 y=2 x+y=2+2=4(False) 2<5 m=7+2=9 y=2+1=3 x+y=2+3==5 (break) x=x+1=2+1=3 3<4 y=1 y<5 m=m+y=9+1=10 y=1+1=2 x+y=3+2==5(break) x=3+1=4 x<4 (False) print (m) 10
29th Jan 2022, 7:40 AM
Brian
Brian - avatar
0
Thank you so much for your help! I get confused with variable every iteration. for example the value of x and y variable here. Practicing to learn my mistakes.
30th Jan 2022, 2:02 AM
Knowledge Is Power
Knowledge Is Power - avatar