Why does this print every iteration?

Hi,

I solved the exercise–> [164, 170, 163, 163]. But I wanted to try to solve this also without using the list comprehension. This is my approach:

#second approach
heights = [161, 164, 156, 144, 158, 170, 163, 163, 157]
can_ride_coaster = []

for height in heights:
  if height > 161:
    can_ride_coaster.append(height)
    print(can_ride_coaster)

Output is:

[164]
[164, 170]
[164, 170, 163]
[164, 170, 163, 163]

How can I get the output of only:
[164, 170, 163, 163]

with my code? Any hints?

1 Like

@code_colt
Prints each iteration on screen since print is inside the for loop.
It writes print out of the for loop and then of the same one being identado to the level of the other written lines, for example:

image

It will result:

[164, 170, 163, 163]

Greetings and successes to you!

Note: the English language is not my native language, sorry for some involuntary error of grammar.

5 Likes