0
What's wrong in this code? Why do I always get the output as 111?
8 Réponses
+ 4
1st you initialize variable inside the loop before another loop operation taking place
that means the variable will always be 1.
2nd you have wrong identation for the increment. k+=1 are inside j loop, j+1 are inside i loop, and i increment is completely outside of the loop
you may be interested in for loop and range() function
+ 4
# if you use for loop it's like this:
res_for = []
for i in range(1,4):
for j in range(1,4):
for k in range(1,4):
res_for.append((i,j,k))
print(*res_for)
# if you use comprehension it's like this:
res = print(*[(i,j,k) for i in range(1,4) for j in range(1,4) for k in range(1,4)])
+ 2
Błack Jesus❕ properly indent the while blocks and increment i, j, and k at the end of one iteration instead of starting.
https://code.sololearn.com/cdmRvs6WZ7U8/?ref=app
Using for loop:
https://code.sololearn.com/cVXDgD4wRXM7/?ref=app