Answer to Question #298633 in Python for leigh

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.


1
Expert's answer
2022-02-16T13:24:52-0500
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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS