-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathpyramid_of_stars.py
More file actions
31 lines (24 loc) · 958 Bytes
/
pyramid_of_stars.py
File metadata and controls
31 lines (24 loc) · 958 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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()