Prev - #28 Border Drawing | Table of Contents | Next - #30 3D Box Drawing

Exercise #29: Pyramid Drawing

drawPyramid(5)       #
                       ###
                       #####
                       #######
                       #########

This exercise continues the generative ASCII art programs of Exercise #27, “Rectangle Drawing,” and Exercise #28, “Border Drawing.” In this exercise, your code prints a pyramid of hashtag characters in any given size.

Exercise Description

Write a drawPyramid() function with a height parameter. The top of the pyramid has one centered hashtag character, and the subsequent rows have two more hashtags than the previous row. The number of rows matches the height integer. There are no Python assert statements to check the correctness of your program. Instead, you can visually inspect the output yourself. For example, calling drawPyramid(8) would output the following:

       #

      ###

     #####

    #######

   #########

  ###########

 #############

###############

Try to write a solution based on the information in this description. If you still have trouble solving this exercise, read the Solution Design and Special Cases and Gotchas sections for additional hints.

Prerequisite concepts: strings, string concatenation, string replication, for loops, range()


 

Solution Design

It’s important to notice the general pattern as the height of the pyramid increases. Here’s a pyramid of height 1:

#

Here’s a pyramid of height 2:

 #

###

Here’s a pyramid of height 3:

  #

 ###

#####

In order to center the pyramid correctly, you need to print the correct amount of space characters on the left side of the row. Here’s the pyramid with height set to 5 and the spaces marked with periods to make them visible:

....#

...###

..#####

.#######

#########

The number of hashtag characters begins at 1 for the top row and then increases by 2. The code in the for loop requires a number of hashtags equal to rowNumber * 2 + 1. The following table shows the number of spaces and hashtags for a pyramid of height 5:

Row Number

Number of Spaces

Number of Hashtags

0

4

1

1

3

3

2

2

5

3

1

7

4

0

9

Notice that for all pyramids, the number of spaces for the top row is height - 1, and each subsequent row has one less space than the previous row. An easier way to create each row is with a for loop that ranges from 0 up to, but not including, height for the row number, where row 0 is at the top. Then the number of spaces at a row number is height – (rowNumber + 1).

String replication can easily create the # hashtag and space character strings. In Python, you can use the * operator with a string and an integer to evaluate to a longer string. For example, enter the following into the interactive shell:

>>> 'Hello' * 3

'HelloHelloHello'

>>> '#' * 7

'#######'

>>> rowNumber = 5

>>> (rowNumber * 2 + 1) * '#'

'###########'

Special Cases and Gotchas

If our solution uses a for loop to loop over the range from 0 up to, but not including, height, we don’t need a separate check for a height of 0 or a negative height. This is because these values cause the for loop to not run its code, resulting in no output.

Now try to write a solution based on the information in the previous sections. If you still have trouble solving this exercise, read the Solution Template section for additional hints.

Solution Template

Try to first write a solution from scratch. But if you have difficulty, you can use the following partial program as a starting place. Copy the following code from https://invpy.com/pyramiddrawing-template.py and paste it into your code editor. Replace the underscores with code to make a working program:

def drawPyramid(height):

    # Loop over each row from 0 up to height:

    for rowNumber in range(____):

        # Create a string of spaces for the left side of the pyramid:

        leftSideSpaces = ' ' * (____ - (rowNumber + ____))

        # Create the string of hashtags for this row of the pyramid:

        pyramidRow = '#' * (____ * 2 + ____)

        # Print the left side spaces and the row of the pyramid:

        ____(leftSideSpaces + pyramidRow)

The complete solution for this exercise is given in Appendix A and https://invpy.com/pyramiddrawing.py. You can view each step of this program as it runs under a debugger at https://invpy.com/pyramiddrawing-debug/.

Prev - #28 Border Drawing | Table of Contents | Next - #30 3D Box Drawing