Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

This page removed for FERPA compliance
View this PageEdit this PageUploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

For Loops Problem 14 Solution

Write a function that takes in a number. Print a pyramid that is that many "rows" tall. Kind of like this...

def aliensMadeThePyramids(num):

  # Assuming that each row will be two rows longer than the previous, then
  # that means the bottom row will have 2*n + 1 "bricks" in it and zero 
  # spaces in front of it

  # ...and the one before that will have 1 space in front of it...

  # ...and the one before that will have 2 spaces in front of it...

  # ...and the top row will have num spaces in front of it...

  # so lets start out our number of spaces at num and our number of
  # bricks with 1
  spaces = num
  bricks = 1

  # Now we'll make a for loop that'll go from the top to the bottom.
  # Note: This is not a good way to construct a pyramid in real life.

  for layer in range(num):

    print spaces * " " + bricks * "t"

    # now we ensure that the next row will have two REMOVED bricks in it...
    bricks = bricks + 2

    # and that the next row will have one less space in it...
    spaces = spaces - 1


Link to this Page