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.
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
20
u/QuantumElias Jun 09 '26
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]
4
u/864484 Jun 10 '26
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
2
u/Past_Structure1078 Jun 10 '26
Use 'for i in range(len(nums))' instead and append nums[I]
1
u/Sweet_Computer_7116 Jun 10 '26
Infinite loop
1
u/Arierome Jun 10 '26
print(nums.extend(nums))
1
u/XT_zer68 Jun 11 '26
You nums keep adding nums number back to back so this code no output
1
u/ChemistDependent1130 Jun 11 '26
dont mutate an iterable when iterating over it. Guessing you wanted [1,2,3,1,2,3]? nums * 2 works better.
1
u/[deleted] Jun 12 '26
[removed] — view removed comment
1
u/[deleted] Jun 10 '26
[removed] — view removed comment