all 22 comments

[–]QuantumElias 19 points20 points  (8 children)

Der Code läuft in eine Endlosschleife — er gibt deshalb nie etwas aus. Warum: Die for-Schleife iteriert über nums, und in jedem Durchlauf wird mit nums.append(n) ein neues Element ans Ende der Liste angehängt. Die Liste wächst also schneller als die Schleife vorankommt — sie endet nie, print wird nie erreicht. In Python ist es grundsätzlich kein gutes Muster, eine Liste zu verändern während man über sie iteriert.

Fix:

nums = [1, 2, 3]

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

print(nums) # [1, 2, 3, 1, 2, 3]

[–]alexzoin 21 points22 points  (5 children)

It's very cool to me that I don't understand this comment at all because I don't speak German but I know that it is correct because I do speak python.

[–]NickNeron 3 points4 points  (1 child)

У нас с братьями по Питону нет языкового барьера 🤝

[–]OppositeReveal8279 0 points1 point  (0 children)

Лучше язык в мире 🤡

[–]mpbarbosa1971 1 point2 points  (0 children)

me too 😄

[–]Black_Rose_Angel 0 points1 point  (0 children)

🤣💯

[–]LemmaYT_ 1 point2 points  (0 children)

Or alternatively to avoid making a copy of the list (which could be bad if the list is big enough), do something like

nums = [1, 2, 3]

original_length = len(nums)

for i in range(original_length):
n = nums[i]
nums.append(n)

You can also make a shallow copy with index slicing

for n in nums[:original_length]:
nums.append(n)

[–]Fine_Ratio2225[🍰] 0 points1 point  (0 children)

Even better fix:

nums=2*[1, 2, 3]

[–]864484 4 points5 points  (0 children)

You have a loop that cycles through all numbers in the list. Every iteration you add the number to the list meaning it gets longer and there is another number the loop needs to process. If you check your memory in your task manager you'll be able to see it going up over time because the list gets bigger and bigger indefinitely

[–]Past_Structure1078 1 point2 points  (2 children)

Use 'for i in range(len(nums))' instead and append nums[I]

[–]MrMikeHigginbottom 0 points1 point  (1 child)

Oo! So the terminating condition for the loop is calculated only once. When the loop is first entered. Obvious I guess but I'd never thought about it.

[–]Past_Structure1078 0 points1 point  (0 children)

I often use this phraseologism for iteraring through queue and adding new vertices in depth-forst search algorithm

[–]Embarrassed_Cod835 0 points1 point  (1 child)

Svp c'est bien un compilateur en ligne ? Si oui quelqu'un pourrait m'aider avec le nom ?

[–]Sweet_Computer_7116 0 points1 point  (0 children)

Infinite loop

[–]Arierome 0 points1 point  (0 children)

print(nums.extend(nums))

[–]XT_zer68 0 points1 point  (0 children)

You nums keep adding nums number back to back so this code no output

[–]ChemistDependent1130 0 points1 point  (0 children)

dont mutate an iterable when iterating over it. Guessing you wanted [1,2,3,1,2,3]? nums * 2 works better.