(10 pts) Complete Programming Exercise 3.2 on page 72 of Zelle. Again include appropriate comments. Name your program area.py. Your program should:
range
.
Write a Python range
expression for each of
the following lists. You should write your expressions in a new file, rangeAndList.py,
in IDLE, using print
statements to print the answers.
Answers to the first two are given as examples.
[0, 1, 2, 3, 4, 5]
print "4a.", range(6)
[2, 3, 4, 5, 6, 7]
print "4b.", range(2, 8)
[-2, -1, 0, 1, 2]
[1, 3, 5, 7, 9]
[1, 4]
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
[2, 1, 0, -1, -2]
Manipulating Lists. Continuing with the rangeAndList.py file you created for the previous problem, add the following line of code:
myList = [3, 6, 8, 11, 7, 4, 5, 9, 10, 0, 2]
Now add statements to your program that print each of the
following numbers or lists using expressions involving myList
.
Again, a couple of examples are given.
3
print "5a.", myList[0]
[3, 6, 8]
print "5b.", myList[:3]
[8, 11, 7, 4]
2
[0, 2]
4
in myList
. myList
. Upload the file containing your answers to problems 3 and 4 to the RangeAndList dropbox in the Homework 4 folder on ANGEL.
Some Geometry with Lists. For this problem you'll write three small, separate but similar, graphics programs: Pizza.py , Polygon.py , and Star.py.
Each program prompts the user to enter an odd integer, number. Then the programs draw pictures as follows:
The pictures below show what the display of your programs should look like for a couple of cases.
Copy and paste liberally during this assignment, but get Pizza right before moving on to Polygon and Star.
You may wish to use the following code, which generates an appropriate list of Points for any value of number:
# Some constants radius = 150 centerX = 200 centerY = 200 # Builds vertices list vertices = [] for i in range(number):
x = radius * cos(2 * pi * i / number) + centerX
y = radius * sin(2 * pi * i / number) + centerY
vertices.append(Point(x, y))
Do you see how you can iterate over the Points stored in the list vertices in order to find endpoints for the lines?
When you have finished and tested your programs, submit each Python source file (Pizza.py, Polygon.py, Star.py) to the corresponding drop box in the Homework 4 folder on ANGEL. (The Star Dropbox is in the HW 5 folder).
If you need help, recall that in addition to your instructor, the student assistant lab hours are a resource for you. The times are listed in the Course Syllabus.