+ 1
Help me with the Pyramid code pleaseTT
import random line=5 def pyramid(line): #pyramid creating for each in range(1,line+1): yield ' '*(line-each) for i in range(1,each+1): yield str(random.randint(1,9)) yield '\n' Q=pyramid(line) print(' '+' '.join(Q)) for i in Q: #iteration print(i) ââââââââââ- This code should print both Pyramid code And iteration of the pyramid But iteration doesnât print anything For ex) 3 This is what i expect 45 217 5986 09137 3 4 5 2 1 7 ... ________________
2 Answers
+ 4
# I would suggest to modify your generator function to yield lines instead of spaces and numbers separatly, and convert the Q instance in a real list, to be able to iterate over it more than once:
import random
line=5
def pyramid(line): #pyramid creating
for each in range(1,line+1):
ln = ' '*(line-each)
for i in range(1,each+1):
ln += str(random.randint(1,9))+' '
yield ln
Q=list(pyramid(line))
print('\n'.join(Q))
for i in Q: #iteration
print(i)
0
I get your point. Thank you!