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".
Note: Any page numbers mentioned in this lab are from this reading!
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.
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.
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.
* * * * * * *The user should enter the number of asterisks to put along the diagonal. Your function should look like Program 4-20 on p. 158.
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.
rectangularPattern
function would take the width and
height as parameters, and would no longer ask the user for these
values.
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.
range
function printing to create additional patterns of asterisks.
****** ***** **** *** ** *Add a print statement and a call to this new function in your main function.
** * * * * * * * * * *Add a print statement and a call to this new function in your main function.
******** ******** ******** ******** ********Add a print statement and a call to this new function in your main function.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *Add a print statement and a call to this new function from your main function.
Do ONE of Exercises 5 and 6 below:
* ** *** **** ***** ******Add a print statement and a call to this new function in your main function.
****** ***** **** *** ** *Add a print statement and a call to this new function in your main function.
******** * * * * * * * * * * * * * * * * ********Add a print statement and a call to this new function in your main function.
* * * * * * * * * * * * * * * ** * * ** * * * * * * * * * * * *Add a print statement and a call to this new function in your main function.
* * * * ** ** ** ** *** *** *** *** **** **** ******* ********** *** *** ********** ** ** **** **** * * *** *** ** ** * *Add a print statement and a call to this new function in your main function.