The purpose of this assignment is to give you practice with tracing Python programs and writing your own programs. You will practice with variables, if-statements, functions, and with the processing of images.
You and your partner should spend at least one hour reading through this handout to make sure you understand what is required.
The program graphic.py
produces and displays a tiny graphic image.
Notice that
the lines of code do not execute from top the top of the file to the bottom,
and some lines execute multiple times.
In a file called trace.txt
, list the
line numbers of graphic.py
in the order in which they execute.
If a line is executed
multiple times, list it multiple times.
Each time line 8 is executed, specify the current
values of x
and y
.
In order for us to tell whether or not you traced the program correctly, you
must follow some strict rules about the format of your file
trace.txt
:
x
and y
on a separate line,
like this:x:__, y:__
graphic.py
should be listed;
do not list lines from the functions in module
media
that it calls.
In the debugger, you can avoid going into those by using
Step Over rather than Step Into (and if you accidentally
do go into them, use Step Out to get back out).
You will need to use Step Into once, to get into
function create_graphic()
.trace.txt
as plain text. To guarantee that
trace.txt
is plain text, use Wing as your editor.
If you don't use Wing and we can't read your file, then we will not mark
this portion of the assignment; you will receive a zero for Part 1.
trace.txt
might look like this:
7 8 x: 6, y: 19 11 2 8 x: 1, y: 42(The numbers in this example are not correct! The example here only to show you the correct format.)
Note that I made the size of the graphic image very small -- variable
dimension
is set only to 3, meaning that the resulting
image is only 3 pixels by 3 pixels in size.
This was just so that your trace wouldn't be too long and tedious.
You might find it interesting to make a copy of the program with
a larger dimension size and see what the graphic looks like when
you run the modified program.
Write the following functions. Include a docstring comment for
each comment.
Work on your functions one by one. Test and debug each before going
on to the next one. Working in separate files during development
might be easier. Be sure to include a docstring for each function.
Write extra functions to make your code more readable. If your code
is more readable, you'll solve the problem sooner - you'll understand
better what you are doing!
After you are done verifying your functions, but them and the extra
functions into one file, called functions.py
.
This is the file you will submit.
Function Name | Description |
---|---|
typing_speed(words, seconds)
|
Given an integer (with value at least 0) indicating a number of words
typed and another integer (with value at least 1) indicating the number of seconds
it took to type that many words, return the
typing speed, in words per minute (as a float ).
|
month(code)
|
Given an string representing a date in yymmdd format, where the year is at least 1, return the month number, an int between 1 and 12. (Hint: You don't need to use strings for this. Convert 'code' into an int. Division by numbers that are powers of 10, and modulo by numbers that are powers of 10, will be very helpful.) |
average_red(pic)
|
Given a picture pic ,
return a float that is
the average red value among all its pixels.
|
average_intensity(pic)
|
Given a picture pic ,
return a float that is the average intensity value among all its pixels.
The "intensity" of a pixel is the sum of its red, green,
and blue values.
|
warhol(pic)
|
Given a picture pic , change each pixel
whose intensity is less than the average in that picture
into media.yellowgreen ,
and each pixel whose intensity is at least the average into
media.powderblue .
Do not return any value.
|
add_border(pic, thickness, color)
|
Given a picture pic , add a border that is
thickness pixels wide and in the given
color .
Do not return any value.
The parameter thickness is an integer that is at least 0, and
at most the height or width of the picture, whichever
is smaller.
The parameter color is one of
the colors defined in
the media
module.
Hint: Functions
add_rect_filled ,
get_height , and get_width
from media will be helpful.
Also, while debugging your code, you might find it useful to use
media.create_picture to create your own
simple, small picture to work with.
|
Write a program in a file called frame.py
that allows a
user to "frame" a picture by adding a border around it.
You may use any of the functions from Part 2 in your solution;
you can import them into your framing program with the statement
import functions
.
You will need to use the function raw_input()
, which you have
seen in class. Recall that it prompts a user
to type something, waits for the user's input, and then returns what
they typed.
Your program should do the following:
raw_input()
to get the filename of the picture the user wants to frame and
media.load_picture()
to load it.
raw_input()
to prompt the user for the following:
media.black, media.white, and media.gray.
(Note that media
spells that last color with an "a"
rather than an "e".)
Use media.save()
to save the modified picture
(with its new border) in the same file it came from.
Here is a sample interaction where user input appears in bold. You must use this format, including the exact wording, punctuation, and upper/lower case letters:
Picture to frame: IMG_7440.JPG Border width: 25 Border color (b/w/g): g
If the user gives an invalid color choice (anything other than "b",
"w" or "g"), print the message Invalid color choice.
and
do nothing more.
media.choose_file()
, for example.
And
your program must have
absolutely no output other than the output described above --
no messages to the user (not even a
"welcome!") and no displaying of pictures.
These are the aspects of your work that we will focus on in the grading:
Correctness: Your functions and your framing program should perform as specified. Correctness, as measured by our tests, will count for the largest single portion of your marks.
Formatting style: Make sure that you read the style rules page for some general rules and guidelines about formatting your code.
Programming style: Your variables names should be meaningful and your code as simple and clear as possible.
Hand in the following files:
trace.txt
functions.py
frame.py