Question #298633

Write a program that draws the three shapes as illustrated in figure 4.2. Think about the concept of composition discussed in chapter 3. Create several smaller functions to perform the basic tasks. Use those functions to build more complicated functions until you have achieved the stated goal.


Expert's answer

def drawTriangle(n):
    triangle = []
    if n % 2 == 1:
        for i in range(n):
            triangle.append(' ' * i + '*' * (n - i * 2) + ' ' * i)
        triangle.reverse()
        for i in triangle:
            print(i)
        print('\n')
    else:
        print('N must be odd to draw a triangle')

def drawRectangle(a, h):
    for i in range(h):
        print('*' * a)
    print('\n')

def drawRhombus(n):
    if n % 2 == 1:
        for i in range(1,n,2):
            str = ' ' * ((n-i)//2)  + '*' *i + ' ' * (n-i)
            print (str)
        for i in range(n,0,-2):
            str = ' ' * ((n-i)//2)  + '*' *i + ' ' * (n-i)
            print (str)
        print('\n')
    else:
        print('N must be odd to draw a rhombus')

drawTriangle(11)
drawRectangle(10, 5)
drawRhombus(11)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS