Homework Assignment
CS 121 (CS1)
- Homework 3 Due Monday, February 17, 2014
-
-
Turtle graphics [c]
midpoint --
Write a program using Turtle Graphics that draws a line segment then stamps a circle at the midpoint of that line segment. Submit your answer as a screenshot that shows both your code inidle3
and the drawing window displaying the image. Your final image should look something like this:
Notes:
-
Your code must start with
import turtle
which makes turtle graphics operations available. -
You will need to create a graphics window to draw on:
win = turtle.Screen()
Notice that the window doesn't need to be namedwindow
or any other particular variable name. You can choose a different name other thanwin
if you wish. -
Next, create a turtle variable
tom
(or a name of your choice) usingtom = turtle.Turtle()
You could also name your turtleTom
instead oftom
, but we usually start variable names with lower case letters (in order to make it easier to distinguish variable names, liketom
, from other kinds of names, such asTurtle
orScreen
). -
Now draw the line segment. Choose a direction using
tom.right()
ortom.left()
(with some argument value, e.g.,tom.right(45)
), then use a call oftom.forward()
(with some argument value) to draw the line. -
To draw the midpoint, you will need to position your turtle
tom
at that midpoint. One way to accomplish this is to reverse direction usingtom.right()
ortom.left()
, then gotom.forward()
by half the length you travelled before. (If you can think of another way to get to that midpoint, you may use that approach instead.) -
To draw a point at the current location, you can use
tom.dot()
-
Optional: You can hide the turtle shape (the arrow) using
tom.hideturtle()
.The Python3 technical documentation for turtle graphics contains this and many other options for using turtles.
-
path --
Write Python code that draws a path of line segments, five or more segments long, using turtle graphics. Each segment in the path should be drawn in a different color. Submit your work as a screenshot showing both code and image, as before.Notes:
-
The
color()
method (operation) for a turtle changes that turtle's drawing color. For example, iflisa
is a turtle, thenlisa.color('green')
changes that turtle's drawing color to green. -
You can start a new
idle3
for a fresh start on this program.
-
draw --
Design and create a simple drawing of your own using turtle graphics. Be creative! Submit your finished image using a screen shot; include as much of your code as you can show within youridle3
window.
-
Python3 modules
--
Python Modules chapter problems at end of chapter: 2[H]
([H] means to write out the solution on paper and submit in class or in grader box.)
-
More turtle graphics
--
Python Turtle Graphics chapter problems at end of chapter: 2[H], 4, 6, 8[H], 10, 12[H]-
The problems marked [H] should be written out on paper and submitted in class.
-
The other problems are short programs. Submit these via email to
cs1b-graders@stolaf.edu
(copying and pasting your code into an email is sufficient; screenshots not needed for these problems).
- Problem 6
-
-
After you input the values you will need (number of sides, length of sides, colors), you will need to use turtle graphics to draw the polygon. First move
forward
by the length of a side, then turn, then draw again, then turn, etc., until all sides have been drawn. -
Use a
for
loop to draw the correct number of sides. -
You can turn your turtle either
right
orleft
, as long as you always turn the same way while drawing your polygon. The amount to turn (whichever direction you choose) is 360/N, where N is the desired number of sides. For example, to draw a regular 4-sided polygon (i.e., a square), the amount of the turn should be 360/4 = 90.
-
- Problem 8
-
-
The point of this problem is to look at the Python3 code and predict what the turtle will draw, sketching your prediction on paper, then to press "Run" to check your prediction. To see the Python3 code, click on "Show/Hide code."
In case it's helpful, this is the code that should be shown when you click on "Show/Hide code":
import turtle wn = turtle.Screen() tess = turtle.Turtle() tess.right(90) tess.left(3600) tess.right(-90) tess.left(3600) tess.left(3645) tess.forward(-100)
-
Hint about those large left turns: The argument for
left()
is measured in degrees, and 360 degrees moves all the way around a circle, resulting in the turtle pointing in the same direction as it had before the 360-degree turn. It is as if the direction never changed.Use that fact to figure out what turtle direction would result after a 3600 degree turn.
__________ -
-
-
Reading
--
Read online text: Functions
submit at least one reading question by 11am before the next class meeting.
-