Help me with the Pyramid code pleaseTT | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 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 ... ________________

11th Jan 2018, 6:28 AM
Yololab
Yololab - avatar
2 Réponses
+ 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)
11th Jan 2018, 6:52 AM
visph
visph - avatar
0
I get your point. Thank you!
11th Jan 2018, 7:05 AM
Yololab
Yololab - avatar