+ 1

While loop

i=1 while True: if i%3==0: break print([i],end=" ") i=i+1 --------------------------- I am getting 2 diff lists in output. But I want only one list. What should I change in the code?

18th Mar 2019, 6:11 AM
Manpreet Kaur
Manpreet Kaur - avatar
3 Answers
+ 4
Have an empty list prepared, then append <i> into it as the loop iterates. Print the list after loop completed. i, l = 1, [] while True: if i % 3 == 0: break l.append(i) i += 1 print(l)
18th Mar 2019, 7:06 AM
Ipang
+ 3
You can do this too. i = 1 a = [] while True: if i%3 == 0: break a += [i] i += 1 print(a)
18th Mar 2019, 7:09 AM
Abdul S Ansari
Abdul S Ansari - avatar
+ 3
That means I dont need to print "i", instead append i's values in diff list.. Thnks anyways
18th Mar 2019, 7:09 AM
Manpreet Kaur
Manpreet Kaur - avatar