-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstar_tree_while_loops.py
More file actions
49 lines (38 loc) · 1.41 KB
/
Copy pathstar_tree_while_loops.py
File metadata and controls
49 lines (38 loc) · 1.41 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/env python3
'''Prints a triangular tree with it's height provided by the user.
The tree's height and width varies based on user input
Example:
Height = 6
*
***
*****
*******
*********
***********
'''
from custom_modules.get_positive_number_from_user import get_positive_num
def draw_tree(height):
""" Draws a tree of a given <height>
<height> being an integer
"""
row = 0 # Init first row, from the top, to draw
while row < height: # Draws one row of the tree each time its's body executes
# Print Leading spaces;
# as row gets bigger, no. of leading spaces gets smaller
count = 0
while count < height - row:
print(end=' ')
count += 1
# Print out stars, twice the current row plus one:
# 1. No. of stars on the left side of tree = current row value
# 2. Exacltly one star in the center of the tree
# 3. No. of stars on the right side of tree = current row value
count = 0
while count < ((2 * row) + 1):
print(end='*')
count += 1
print() # Move to the next line
row += 1 # Next row
if __name__ == '__main__':
print(__doc__) # Program Greeting
draw_tree(int(get_positive_num())) # Draw a tree with height from user