CPSC120A
Fundamentals of Computer Science I

Lab 7

Nested Loops

Practice Problem #1

Write a Python program that prints out the multiplication table for all Integers in the range [0, 5].

Example Output

0 x 0 = 0
0 x 1 = 0
0 x 2 = 0
0 x 3 = 0
0 x 4 = 0
0 x 5 = 0
1 x 0 = 0
1 x 1 = 1
... omitted for space ...
4 x 5 = 20
5 x 0 = 0
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
Practice Problem #2

Write a Python program that uses nested for loops to print 10 rows of asterisks where each row has one fewer asterisk than the row preceeding it.

Example

$ python3 practice2.py
**********
*********
********
*******
******
*****
****
***
**
*
Pyramids

Nesting for loops can be confusing. Sometimes, it is better to have a simple example to get a better understanding of how things work. However, we can't let you fully off the hook, mathematically. Printing a pyramid that looks pretty can be slightly challenging, but if you keep track of the number of spaces and letter necessary you should figure out mathematically what is going on.

Details

In Emacs, create a Python program in a file called pyramid.py that prints to the terminal a pyramid of asterisks. It should prompt the user for the number of levels to print.

Test your program by running it multiple times with different input values. Make sure your code follows the courses code conventions.

Example

$ python3 pyramid.py
Enter the number of levels: 6
      * 
     * * 
    * * * 
   * * * * 
  * * * * * 
 * * * * * * 

Hint

  • This program is going to need a total of 3 loops, two loops nested inside another loop. The outer for loop will execute for the number of levels in the pyramid. The second for loop prints the spaces that preceed the askerisks on that level. The third loop prints asterisks.

  • The number of askerisks is directly related to the level of the pyramid. If the top of the pyramid is level 1, then the number of asterisks is the same level number.

  • The number of spaces is inversely related to the level of the pyramid. If the top of the pyramid is level 1, then the number of spaces is the number of levels minus the level number.

Challenge

Using turtle and the stamp method, create a graphical version of the pyramid.

Nautilus

A Nautilus is a sea creature whose shell is very well studied in the field of mathematics. As it turns out, a nautilus shell exhibits properties not only of the golden ratio, but also of fractal like patterns. Today, you will use a nested for loop to replicate the visual appearance of a nautilus shell.

Details

In Emacs, create a Python program in a file called nautilus.py that uses turtle graphics to draw a series of rotated squares to approximate a nautilus shell.

Example

$ python3 nautilus.py

Hint

This program requires the use of a nested for loop.

  • The outer loop determines how many squares are drawn.

  • The inner loop should draw a single square. The outer loop iterator variable should be used to determine the size of the square.

  • After drawing the square, but still but still inside the outer loop, the turtle should turn so that the next square will is slightly rotated.

Challenge

Edit your code so that a user can specify a location on the turtle window to draw the turtle from. In addition, let the user specify the color of the turtle to use in drawing the Nautilus shell.

Submission

Please show your source code and run your programs for the instructor or lab assistant. Only a programs that have perfect style and flawless functionality will be accepted as complete.