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:
the output is:
the output becomes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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 : break |
Output:[1]
[1, 1]
[1, 1, 1]
However when I tried to append the yield result further, like below 1 2 3 4 5 6 7 8 9 10 11 12 |
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) |
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?