-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab5.py
57 lines (49 loc) · 1.52 KB
/
lab5.py
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
48
49
50
51
52
53
54
55
56
57
# 8.11 LAB: Drawing a right side up triangle
#
# Write a recursive function called draw_triangle() that outputs lines of '*' to form a right side up isosceles triangle. Function draw_triangle() has one parameter, an integer representing the base length of the triangle. Assume the base length is always odd and less than 20. Output 9 spaces before the first '*' on the first line for correct formatting.
#
# Hint: The number of '*' increases by 2 for every line drawn.
#
# Ex: If the input of the program is:
#
# 3
#
# the function draw_triangle() outputs:
#
# *
# ***
#
# Ex: If the input of the program is:
#
# 19
#
# the function draw_triangle() outputs:
#
# *
# ***
# *****
# *******
# *********
# ***********
# *************
# ***************
# *****************
# *******************
#
# Note: No space is output before the first '*' on the last line when the base length is 19.
#
# Solution:
def draw_triangle(base_len):
# base case draws the tip of the triangle
if base_len == 1:
print (" " * 9 + '*')
# recursive case
else:
# recursive call (we want the tip drawn first)
draw_triangle(int(base_len - 2))
# printing script (not casting as int breaks this)
print (" " * (9 - int(base_len)//2) + '*' * int(base_len))
# End solution
if __name__ == '__main__':
base_length = int(input())
draw_triangle(base_length)