Nested Loops

Printing patterns

Create a new Python program and name it Rectangle.

Pattern #1: Creating a Rectangle

Get two values from the user: the height and width of the rectangle. The program should printa rectangle of stars that is the given height and width.

For example, if the input is 4 for the height and 6 for the width, your program should print this:

* * * * * *

* * * * * *

* * * * * *

* * * * * *

For example, if the input is 4 for the height and 4 for the width, your program should print this:

* * * *

* * * *

* * * *

* * * *

Note that there is a space after each star just to make the spacing look better. If we didn't print the space, the "square" would look like this:

****

****

****

****

Which isn't really very square. So my print commands look like this:

Method 1:

print("* ")

But the print command puts a new line character at the end of the output. We don't want this. We can change the last character that gets printed with a print statement (the "end" character) like this:

print("* ", end="")

Instead of printing a new line character at the end of the line, it will print an empty string (nothing). This will keep the cursor on the same line, though.

Solution

Problem-solving is looking for patterns. It may be easier to see patterns if we write a few things down. Create a table that describes each row and lists all of the related variables:

Row / Height / Width / Number of stars

It is obvious that the number of stars is always the same on each row – the same number of stars as Width.Below is the easiest way to solve the problem.

height = int(input("Enter height: "))

width = int(input("Enter width:"))

for r in range(height):

print("* " * width)

However, this lesson is about nested loops (loops inside of loops), so I want you to replace the print statement with another for loop that will print one star each time, like this:

print("* ", end="")

So the cursor will stay on the same line after printing the asterisk. New code:

for r in range(height):

for c in range(width):

print("* ", end="")

print()

Note that the final print moves the cursor to a new line.

Pattern #2: Creating a right triangle (left side)

Write code that inputs a positive integer representing the number of asterisks in the base of a triangle and draws the triangle with one star on the first row, two stars on the second row, three on the third row, etc.

For example, if the input was 4, the output would be:

*

* *

* * *

* * * *

Solution

Again, create a table and look for patterns:

row / height / stars

It is obvious that the row number determines how many stars to print.

Pattern #3: Creating an upside-down triangle

Do the same thing as the last problem, but invert the triangle, with the longest line of stars at the top.

For example, if the number input by the user is 4:

****

***

**

*

Solution

Create a table and look for patterns:

row / rows / stars

Pattern #4: Creating a right triangle (right-side)

Do the same as the first problem, but with the right angle at the bottom right side of the triangle rather than at the bottom right.

For example, if the number input by the user is 4:

*

**

***

****

Solution

Create a table and look for patterns:

row / rows / blanks / stars

Note that there will be two inner loops: one for blanks and one for stars.

Pattern #5: Creating a pyramid

The user will enter the number of rows.

Examples

Number of rows is 2:

*

***

Number of rows is 3:

*

***

*****

Number of rows is 4:

*

***

*****

*******

Make a table. Write the row number in the first column. In the second column, write the number of stars. In the third column, write the number of leading spaces. Then derive formulas for the number of stars and the number of leading spaces. Then write your algorithm.

Solution

Create a table and look for patterns:

row / rows / blanks / stars

12/18/2018005-Even-More-Loops.docxPage 1 of 5