What's wrong in this code? Why do I always get the output as 111? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What's wrong in this code? Why do I always get the output as 111?

https://code.sololearn.com/cMxVSqP96Y78/?ref=app

16th Oct 2019, 1:40 PM
Rushi
4 Answers
+ 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
16th Oct 2019, 1:50 PM
Taste
Taste - avatar
+ 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)])
16th Oct 2019, 4:13 PM
Lothar
Lothar - avatar
+ 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
16th Oct 2019, 2:18 PM
R_3-dr
R_3-dr - avatar