As usual, create a directory to hold today's files. All programs that you write today should be stored in this directory.
$ cd ~/cs120/labs $ mkdir lab8 $ cd lab8
Write a Python program that uses nested for loops to print the numbers 1 through 9 in three rows. The numbers should be separated by spaces.
$ python3 practice1.py 1 2 3 4 5 6 7 8 9
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.
$ python3 practice2.py ********** ********* ******** ******* ****** ***** **** *** ** *
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.
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.
$ python3 nautilus.py
Simply repeating this shape several times can create even more
interesting patterns than the one you see above. Add another loop
to your program which will cause the above pattern to execute 4
times. Each nautilus should start at a 90° offset from where
the previous nautilus started. Use this outer loop to modify the color of the turtle for
an even more impressive design. You may need to
use setheading
for this activity.
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.
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.
$ python3 pyramid.py Enter the number of levels: 6 * * * * * * * * * * * * * * * * * * * * *