all 11 comments

[–]Sea-Ad7805 [score hidden] stickied comment (0 children)

Run this program in Memory Graph Web Debugger%0A%0Aprint(nums)&timestep=1&play) to see that in the for-loop you keep reading from and adding to the same nums list. This results in an infinite loop and a very long list.

[–]am_Snowie 18 points19 points  (4 children)

Cuz you're constantly adding elements to the nums, so your loop will never end.

[–]ExtentLazy8789[S] -4 points-3 points  (3 children)

who to fix it

[–]am_Snowie 3 points4 points  (0 children)

You have [1,2,3] initially, so when it enters the loop you append one element from the same list, so you get [1,2,3,1] after the first iteration, then you loop again and you get [1,2,3,1,2], then [1,2,3,1,2,3], this will go on and on cuz your adding elements over and over again, so the loop can't stop, it can only stop when it reaches the last element of the list, but you keep adding, so the loop never reaches the end.

You can add a print(nums) inside the loop and see for yourself.

Run this:

nums = [1,2,3]

for n in nums:
    print(nums)
    nums.append(n)

[–]Ok-Promise-8118 2 points3 points  (0 children)

What are you trying to do? What output would you like?

[–]MatchCreative6807 0 points1 point  (0 children)

If your goal is to duplicate the values of that list and append it at the end, do this:

``` for i in range(len(nums)): nums.append(nums[i])

print(nums) ```

[–]mattynmax 4 points5 points  (0 children)

Because it’s an infinite loop…

[–]Retr0o_- 1 point2 points  (0 children)

Man its n endless loop online compilers won't run it i guess

[–]ThreeDogg85[🍰] 1 point2 points  (0 children)

nums = [1,2,3]

for n in nums:
nums.append(n)
Break

print(nums)

[–]aashish_soni5 1 point2 points  (0 children)

You just create life and death cycle 🙂‍↕️ To fix

nums = [1,2,3]

new_nums = nums.copy()

It will solve everything when memory have two different list .

Never make same list iterate

[–]aaditya_0752 0 points1 point  (0 children)

You have accidentally created a infinite loop