Python - Comment imprimer une pyramide
Un exemple simple de Python pour imprimer une demi-pyramide et une pyramide complète, juste pour le plaisir.
def half_pyramid(rows):
print('Half pyramid...\n')
for i in range(rows):
print('*' * (i+1))
def full_pyramid(rows):
print('\nFull pyramid...\n')
for i in range(rows):
print(' '*(rows-i-1) + '*'*(2*i+1))
def inverted_pyramid(rows):
print('\nInverted pyramid...\n')
for i in reversed(range(rows)):
print(' '*(rows-i-1) + '*'*(2*i+1))
half_pyramid(5)
full_pyramid(5)
inverted_pyramid(5)
Sortie
Half pyramid...
*
**
***
****
*****
Full pyramid...
*
***
*****
*******
*********
Inverted pyramid...
*********
*******
*****
***
*