Do the following exercises.
For problems 3 and following, use today's project: 10-DecisionStructures
- Complete the assigned reading for the next session:
Zelle, Chapter 8 — Loop Structures and Booleans (27 pages).
- (26 points) Complete the ANGEL quiz over this reading,
at the course ANGEL page,
under Lessons → Homework → Homework 10 → Loop Patterns and Booleans.
- (15 Points)
Complete the exercise that you started in class in the
02-countPassFail.py module,
per the TODO's in the module.
- (40 Points)
Do the exercise in the
03-circleOfCircles.py module,
per the following instructions:
Create a function called
circleOfCircles
that takes five parameters (as described below) and draws n circles
whose centers are equally spaced around a (usually) larger circle,
as in this example where n is 12:
Note that, per the above example, you do NOT
actually draw the larger circle that the n circles are spaced around.
The parameters must be as follows, in the order listed:
- A GraphWin object onto which the function will draw the circles.
- n, the number of circles to draw.
- A Point that is the center of the “big” circle that the other circles surround.
- The radius of the “big” circle.
- The radius of each of the “small” circles that your function draws.
Additional requirements:
- If the entire “circle of circles” will NOT fit inside the GraphWin that it is given,
the circleOfCircles function
must print an appropriate message to the console
instead of drawing the “circle of circles.”
- Hint 1: use an IF statement!
- Hint 2: you will want to use the getHeight and getWidth methods
that a GraphWin has.
- With this and the other additional requirements, the graphics window SHOULD appear — that's OK!
But your circleOfCircles function
must print an appropriate message to the console
instead of drawing the “circle of circles”
if the input is bad, per these additional requirements.
- If the parameter n is NOT greater than 2,
the circleOfCircles function
must print an appropriate message to the console
instead of drawing the “circle of circles.”
- Optional (but easy), in case you are wondering how to do more input-validation:
If the parameter n is NOT an integer,
the circleOfCircles function
should print an appropriate message to the console
instead of drawing the “circle of circles.”
- Hint 1: here is how to do this, where n is the variable being used for the parameter:
if type(n) != int:
# Print the error message to the console
elif:
# ...
- Hint 2: Should this IF statement be before or after the IF statement that tests
whether the parameter n is greater than 2? It matters!
We supplied a displayOneCircleOfCircles that main calls to test your
circleOfCircles function.
The first test will display the example “circle of circles” shown above,
if your implementation is correct.
You must supply ADDITIONAL tests in main as appropriate.
Finally, note that tracing out a circle is easy if you simply use polar coordinates.
For an example of using polar coordinates,
see the generatePointsOnCircle function
in the 3-circlePictures.py module
in the 05-TypesAndLists project.
- (40 Points - 10, 10 and 20 for the three functions that you write)
Do the exercise in the
04-mutantFunctions.py module,
per the TODO's in the module.
- In this exercise, you will write functions that MUTATE their parameters
as well as functions that RETURN a value.
- For a good example of MUTATING versus RETURNING,
see the addOneToAll function
and the returnAddOneToAll function
in the 04-mutators.py module
in the 09-MoreFunctions project.
- Here are more examples and discussion of mutators
if you struggle with this problem or just want to see more examples/discussion of mutators.
- (20 points)
Do the exercise in the
05-numerology.py module,
per the TODO's in the module.
-
(20 Points) Version of star function that uses decision
structures. Place all of your function definitions in one Python source file,
06-star.py, already in the
10-DecisionStructures
project.
-
(20 Points)
Create a function called
star() (part of the next HW assignment, but you are
welcome to go ahead and do it)
that takes five parameters and draws an inscribed regular star similar to, but more general than,
Homework 5. Note that Star can be called for an
even number of points, in which case 2 stars may be drawn. The parameters should be in the following order.
-
a GraphWin object on which the function will
draw the star(s)
-
a Point, representing the center of the circle.
-
radius of the circle.
-
number of points in the star---let's call it
n.
-
"skip distance". i.e.,
the number of vertices that we "move
over by" between consecutive vertices of the
star.
- Here is what your star function must do:
It must use a Boolean test to see if
- n
is greater than 2 and
- if n is odd.
If n satisfies both these tests, it will draw the star
following the instructions in step c.
If n
is not greater than 2, it will print an appropriate
message to the console and not draw the star.
If n is
not odd (i.e., n is even), it will do the
following:
- if n/2 is odd, it will draw 2
different colored stars each with n/2
alternating points. (e.g.,
for n == 6, n/2 == 3. In this
case it will draw the star of
David--say one black triangle
with vertices 0, 2, 4 and one
red triangle with vertices 1, 3,
5.) Note: You must use the instructions in step
c. to draw your stars.
- if n/2 is even, it will print
an appropriate message to
the console and not draw the
stars.
-
For the star() function in
Homework 5, the skip distance
was 2 (lines were drawn between every other vertex). A
7-pointed star would have connected vertices 0, 2, 4, 6,
1, 3, 5, 0. A 7-pointed star with skip distance 3 would
connect 0, 3, 6, 2, 5, 1, 4, 0, as pictured below. Also
pictured is a 17-pointed star with skip-distance 5. A
"star" with skip distance 1 is simply a regular polygon.
You might want (but, this is not required) to implement
a polygon function to test this claim (see bonus problem
below).
-
Here is some code that you can use to test your
functions (and make sure that they have the correct
parameters).
def main():
win1 = GraphWin("Window 1", 400, 350)
win2 = GraphWin("Window 2", 400, 350)
win3 = GraphWin("Window 3", 400, 350)
star(win1, Point(250, 50), 35, 7, 3)
star(win2, Point(300, 200), 100, 17, 5)
star(win2, Point(200, 100), 100, 2, 1)
star(win3, Point(100, 200), 80, 6, 2)
star(win3, Point(300, 200), 80, 20, 4)
message = Text(Point(200, 340), "Click this window to end program.")
message.draw(win1)
win1.getMouse()
win1.close()
win2.close()
win3.close()
main()
-
Here is a sample of output from the above code:
-
Bonus:
(5 Points)
Once you have the
star()
function working, can you implement a
polygon()
function that takes the same parameters as the star()
function to just use a single line that invokes your
star()
function.
-
Commit
your
10-DecisionStructures
project to your repository with an
appropriate comment,.
- More Python culture: What's up with the shrubbery examples? Check out this visit of King Arthur to the
Knights Who Say “Ni!”.
|