+ 1

Need help step by step with nested loop

m = 1 for x in [1,2,3]: for y in [3,1,4]: if x == y: m = m * x print (m)

17th Jan 2022, 8:01 PM
Knowledge Is Power
Knowledge Is Power - avatar
9 Respuestas
+ 2
#run this code and see output,, you can understand, if not then you can reply... m = 1 for x in [1,2,3]: print("x =",x) for y in [3,1,4]: print(" y =",y) if x == y: m = m * x print(" x==y, m=m*x:",m//x,'*',x,"=>m =",m) print(m)
17th Jan 2022, 8:16 PM
Jayakrishna 🇮🇳
+ 2
Jayakrishna🇮🇳 ☺️ #Now even I didn't understand ☺️ Try this: m = 1 for x in [1,2,3]: for y in [3,1,4]: print(f"{x}=={y}",x==y) if x == y: print(f"\t\t m={m}*{x}") m = m * x print("print",m)
17th Jan 2022, 8:41 PM
Solo
Solo - avatar
+ 1
Great explanation! I got it. Thank you so much!
17th Jan 2022, 8:26 PM
Knowledge Is Power
Knowledge Is Power - avatar
+ 1
Jayakrishna🇮🇳 , don't pay attention, you have good code, i was just fooling around 😊
18th Jan 2022, 1:10 PM
Solo
Solo - avatar
0
You're welcome...
17th Jan 2022, 8:28 PM
Jayakrishna 🇮🇳
0
😇 Solo What you not understand? Wrong tagged?!! 🤔
18th Jan 2022, 10:51 AM
Jayakrishna 🇮🇳
0
@solo 👍
18th Jan 2022, 2:40 PM
Jayakrishna 🇮🇳
0
#message sending failed. #Arti Kohli so adding here.. m = 0 for x in range (1,3): k = 0 for y in range (-2,0): k = k+y m = m+k print(m) """ for the code : My solution is as follows: output is -10 : you have 2 loop , for x in range(1,3) outer loop runs for x = 1,2 and for every x value inner for y in range(-2,0) loop runs for y=-2,-1 .. as follows : for iteration x=1 m=0 #initially you set these m,k to 0 inner loop k=0 for value y=-2 k = 0+ (-2) k=-2 m=0+(-2) m=-2 y=-1 k=-2+(-1) k=-3 m=-2+ (-3) m=-2-3 m=-5 inner loop end when y=0 now next for iteration x=2 now and inner loop start again k=0 #again set to 0 for value y=-2 k = 0+ (-2) k=-2 m=-5+(-2) m=-7 y=-1 k=-2+(-1) k=-3 m=-7+ (-3) m=-7-3 m=-10 """
20th Jan 2022, 7:48 PM
Jayakrishna 🇮🇳
0
I thank you a lot for answering. I appreciate it! 👍 I see the error now.TY again
20th Jan 2022, 8:27 PM
Knowledge Is Power
Knowledge Is Power - avatar