Last active
December 26, 2021 15:30
-
-
Save guilyx/5e4f333fca69f6d2c04b4da0efa39a15 to your computer and use it in GitHub Desktop.
Pyramid/Triangles patterns printing stars
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Écrivez une fonction Python dont le nom commence par « triangle ». | |
Cette fonction prend un paramètre : le nombre de lignes à écrire. | |
Si le nombre de lignes est pair, la fonction ne doit rien afficher. | |
Sinon elle doit afficher un triangle composé d'étoiles précédées par des espaces. | |
Par exemple, si le nombre de lignes vaut 5, il faut afficher : | |
* | |
*** | |
***** | |
******* | |
********* | |
""" | |
def triangle_star(nb_lines): | |
if nb_lines < 0: | |
print("Failure: Can't have a negative number of line!") | |
return | |
if nb_lines % 2 == 0: | |
return | |
nb_stars = 1 | |
nb_spaces = nb_lines - 1 | |
for i in range(1, nb_lines+1): | |
stars = nb_stars*"*" | |
spaces = nb_spaces*" " | |
print(f"{spaces}{stars}") | |
nb_stars += 2 | |
nb_spaces -= 1 | |
print(triangle_star(5)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment