Dec-04-2024, 05:44 PM
(This post was last modified: Dec-04-2024, 06:13 PM by deanhystad.)
when I try to use print the output the yield result, everything is shown as expected. The code and output is as below:
def triangles():
oo = [1]
yield oo
oo.append(1)
yield oo
oo.append(1)
yield oo
oo.append(1)
yield oo
# Collect the first 3 rows of Triangle
n = 0
for t in triangles():
print (t)
n += 1
if n == 3:
breakthe output is: Output:[1]
[1, 1]
[1, 1, 1] However when I tried to append the yield result further, like below n = 0
results = []
for t in triangles():
## print (t)
results.append(t)
n += 1
if n == 3:
break
# Print the results
for t in results:
print(t)the output becomes: Output:[1, 1, 1]
[1, 1, 1]
[1, 1, 1]Actually it is hard for me to understand the result. why the 1st and 2nd row changes after further appending?
