diff --git a/Pyramid of stars b/Pyramid of stars new file mode 100644 index 0000000000..0220b87ef8 --- /dev/null +++ b/Pyramid of stars @@ -0,0 +1,13 @@ +# Program to create a pyramid of stars + +rows = int(input("Enter the number of rows: ")) + +for i in range(1, rows + 1): + for spaces in range(1, rows - i + 1): + print(end=' ') + + for stars in range(1, i + 1): + print('*', end=' ') + + print() + diff --git a/pyramid_of_stars.py b/pyramid_of_stars.py new file mode 100644 index 0000000000..6967dafc02 --- /dev/null +++ b/pyramid_of_stars.py @@ -0,0 +1,31 @@ +# Program to print a centered full pyramid of stars based on user input + +def get_positive_int(prompt: str) -> int: + """Prompt the user until they provide a positive integer (> 0).""" + while True: + try: + value = int(input(prompt).strip()) + if value <= 0: + print("Please enter a positive integer greater than zero.") + continue + return value + except ValueError: + print("Invalid input. Please enter a valid integer.") + + +def draw_pyramid(rows: int) -> None: + """Print a centered full pyramid of stars with the given number of rows.""" + for i in range(1, rows + 1): + spaces = ' ' * (rows - i) + stars = '* ' * i + # rstrip to avoid trailing space at line end + print(f"{spaces}{stars.rstrip()}") + + +def main() -> None: + rows = get_positive_int("Enter the number of rows: ") + draw_pyramid(rows) + + +if __name__ == "__main__": + main()