+ 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)
9 Answers
+ 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)
+ 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)
+ 1
Great explanation! I got it. Thank you so much!
+ 1
Jayakrishna🇮🇳 , don't pay attention, you have good code, i was just fooling around 😊
0
You're welcome...
0
😇
Solo
What you not understand?
Wrong tagged?!! 🤔
0
@solo 👍
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
"""
0
I thank you a lot for answering. I appreciate it! 👍
I see the error now.TY again