Lab: Nested Loops

 


Introduction

The purpose of this lab is to gain practice working with nested loops. It builds upon the "In the Spotlight: Using Nested Loops to Print Patterns" feature from the Gaddis text, "Starting Out With Python".



Background

  1. If you have not already done so, read through the "In the Spotlight: Using Nested Loops to Print Patterns" feature.
    Note: Any page numbers mentioned in this lab are from this reading!
  2. In the Editor in Spyder, create a new file for the functions you will write in this lab. Then save the new file with a name representative of this lab.
  3. Write a function called rectangularPattern that displays a rectangular pattern of asterisks, based on user input of the number of rows and columns. Your function should look like Program 4-18 on p. 156.
    Stop and Think: Be sure that you understand exactly how this function works before going any further! If there are any parts of this program that you do not understand, stop and ask a TA or instructor.
  4. Test your function by entering different values for the rows and columns and looking to see if the right number of asterisks are printed.
  5. Write a function called trianglePattern that displays a triangle that looks like the following:
             *
             **
             ***
             ****
             *****
             ****** 
        
    The user should enter the number of asterisks that should be in the bottom row. Your program should look very similar to Program 4-19 on p. 157.
  6. Write a function that displays a diagonal, or stair-step pattern that looks like the following:
            *
             *
              *
               *
                *
                 *
                  *
        
    The user should enter the number of asterisks to put along the diagonal. Your function should look like Program 4-20 on p. 158.

  7. Create a new function, called main that contains calls to the previous three functions. For each function, it should print out a statement of which pattern is going to be printed, then calls that function. To get you started, you may copy and paste this version of a main function:
        def main():
            print("Rectangular pattern:")
            rectangularPattern()
    
            print("Triangle pattern:")
            traingularPattern()
    
            print("Diagonal pattern:")
            diagonalPattern()
        
    Now, you could call each function individually to test it, or you could call main(), which would test all of your functions.

  8. Modify your functions above so that they take the dimensions of the pattern as parameters. So, for example, the rectangularPattern function would take the width and height as parameters, and would no longer ask the user for these values.

  9. So where will the user get to specify the dimensions to be used in these functions now? Add input statements at the beginning of the main function to ask the user for the width and height. Then pass these values as actual parameters in the calls to your functions. For functions that work on a square, you may decide whether to pass the width or the height value as the parameter. By doing this, the program only stops once to ask the user for input, which should make the program run more quickly.

Extensions

In these exercises, you will use what you have learned about nested loops, the range function printing to create additional patterns of asterisks.

  1. Write a function that displays the upper triangle of a square. Since we will assume this pattern is created in a square, your function should take either width or height as its parameter. This function creates an upper triangle like the following:
            ******
            ***** 
            ****
            ***
            ** 
            *
        
    Add a print statement and a call to this new function in your main function.

  2. Write a function that displays a sort of upside-down V. We may assume this pattern is created in a square, so your function should take either width or height as its parameter. This function creates a pattern that looks like the following:
            **
            * *
            *  *
            *   *
            *    *
            *     *
        
    Add a print statement and a call to this new function in your main function.

  3. Write a function that will create an every-other-row pattern of asterisks, based on the user's input of the number of rows. Assume this pattern is created in a rectangle, so the function should take width(number of columns) and height(number of rows) as its parameters. If the user specifies that there are 10 rows and 8 columns, 5 of the rows should have 8 asterisks, as in the following:
            ********
    
            ********
    
            ********
    
            ********
    
            ********
        
    Add a print statement and a call to this new function in your main function.

  4. Write a function that creates an every-other-column pattern of asterisks, based on the user's input of the number of columns. Assume this pattern is created in a rectangle, so the function should take width(number of columns) and height(number of rows) as parameters. If the user specifies that there are 8 columns and 10 rows, 4 of the columns should have 10 asterisks, as in the following:
            * * * *
            * * * *
            * * * *
            * * * *
            * * * * 
            * * * *
            * * * * 
            * * * *
            * * * *
            * * * *
        
    Add a print statement and a call to this new function from your main function.

    Do ONE of Exercises 5 and 6 below:

  5. Write a function that creates a bottom right triangle pattern that looks like the pattern below. You may assume this pattern is created in a square, so your function should take either width or height as its parameter, where this number will be the number of asterisks in the largest row.
                     *
                    **
                   ***
                  ****
                 *****
                ******
            
    Add a print statement and a call to this new function in your main function.

  6. Write a function that creates an upper right triangle pattern that looks like the pattern below. You may assume this pattern is created in a square, so your function should take either width or height as its parameter, where this number will be the number of asterisks in the largest row.
                ******
                 *****
                  ****
                   ***
                    **
                     *
            
    Add a print statement and a call to this new function in your main function.

  7. Write a function that creates a border pattern around a rectangle. The fucntion should take the width(number of columns) and the height(number of rows) as parameters. If the width and height are 8 and 10, respectively, the pattern would look like the following:
                ********
                *      *
                *      *
                *      *
                *      *
                *      *
                *      *
                *      *
                *      *
                ********
            
    Add a print statement and a call to this new function in your main function.

  8. Write a function to create a pattern of your own design (different than the challenge patterns below). Decide whether it should be created in a square or rectangluar shape, and pass the appropriate parameter(s). Be sure to include good, descriptive comments before your function, and add a print statement and call to this new function in your main function.

  9. Challenge: (Not required, just for fun) Write a function that creates an X pattern. You may assume the pattern is created in a square (same number of rows and columns). Your function should take either the width or height as its parameter. The pattern would look like the following, given widths of 10 and 7, respectively:
                *        *                      *     *
                 *      *                        *   *
                  *    *                          * *
                   *  *                            *
                    **                            * *
                    **                           *   *
                   *  *                         *     *
                  *    *
                 *      *
                *        *
            
    Add a print statement and a call to this new function in your main function.

  10. Challenge: (Not required, just for fun) Write a function that creates a butterfly pattern. You may assume the pattern is created in a square (same number of rows and columns). Your function should take either the width(number of columns) or the height(number of rows) as its parameter. The pattern would look like the following, given widths of 10 and 7, respectively:
                *        *                      *     *
                **      **                      **   **
                ***    ***                      *** ***
                ****  ****                      *******
                **********                      *** ***
                **********                      **   **
                ****  ****                      *     *
                ***    ***
                **      **
                *        *
            
    Add a print statement and a call to this new function in your main function.

Submit