Lab 2: Advanced Arithmetic, Object-Oriented-Programming, and For Loops
Goals
After the lab, you should know how to
- more advanced arithmetic problems
- using an API to solve problems
- solve basic looping problems in Python
- creating basic web pages to display images
Objective: Review
Review the slides for today.
Objective: Set Up
Run runHelpClient &
Linux: Set up for Lab 2
- Create a directory called
lab2in yourcs111directory. Your programs and the output for this lab will all be saved in thelab2directory. - Copy the
graphics.pyfile from the directory/csdept/courses/cs111/handouts/lab2into yourlab2directory.
Objective: Programming in Python
We'll practice writing several Python programs, each in their own text file. Name the files lab2.1.py through lab2.7.py.
Your programs will be graded on correctness, style, and how well you tested them. Make sure you adhere to the good development and testing practices we discussed in class. Your code should be readable and your output should be useful and well-formatted.
After you've developed a correct solution to each program, restart IDLE or close and reopen the IDLE "shell" by running the program again (using F5), demonstrate that the program works using several good test cases, and save the output to a file named lab2.x.out, where x is the problem number.
You can use Python Visualizer to help you see what is happening in your program. This is the visualizer used in the text book.
- (13 pts) Distributing Greatest Hits Albums. A
band is putting out their Greatest Hits album and needs to
know how many cds their album requires. Bands like the Ramones
can fit a lot more tracks on a cd than a band like Led Zeppelin.
Write a program that takes the number of greatest hits and the size of the cds (in terms of the number of tracks) and determines how many cds are needed and how many tracks will have to wait for the next Greatest Hits album.
This program determines the number of CDs in a Greatest Hits album. How many greatest hits/tracks do you have? 24 How many tracks fit on a cd? 10 Your album requires 2 cds 4 tracks will have to wait for the next Greatest Hits album.
Note: you may have some grammar issues in your output. We don't know how to fix those yet.
- (15) "Mind-blowing Math Trick Can Tell Your Age and Shoe
Size" You may have heard about
this silly
trick. You probably didn't think about implementing it in a
program, but you can!
There are several steps, but they're all relatively simple to implement.
- Ask the user for their shoe size--don't allow them to use half sizes using a trick we learned--and the year they were born.
- multiply the shoe size by five
- add 50 to that number
- take that total and multiply it by 20
- add 1,016 to that total
- subtract the year you were born
The result: the first two numbers of the four-digit number are your shoe size and the last two numbers are your age (if your birthday hasn't happened this year yet). Extract those two numbers and present them to the user.
Example Run:
This program will figure out your shoe size and your age. Enter your shoe size (not half sizes): 9 Enter the year you were born: 1996 Working... Multiply shoe size by five 45 Add 50 to that number 95 Take that total and multiply it by 20 1900 Add 1016 to that total 2916 Subtract the year you were born 920 Your shoe size is 9 Your age is 20
- (13 pts) Using three variables (i, j, and result), assign them
values to calculate and display
result = i % j. Use assignment and print statements and aforloop to show the results of i % j, where i = 6 and j increases from 1 to 8. Example output (without the appropriate values filled in):6 % 1 = ? 6 % 2 = ? ...
- (10) Using the
graphicsmodule, draw a yellow circle with radius 30 and a red square of width 50 in a window that is 400x200 with the name "Practice". Position the circle in the upper-left quadrant of the canvas and the square in the lower-right quadrant.Please add two calls--one to
getMouse()and one toclose()--on yourGraphWinobject at the end of the program. It won't make a difference for you in IDLE, but it does make a difference for me when I run the programs automatically.There will be no IDLE output for this program.
- (10) Using the
graphicsmodule, draw the beginning of a snow-person. Create a canvas with the title "Snow Person". Draw a white circle of radius 50. Clone the circle and move the cloned circle above the first circle (so that the circle appears to be sitting on top of the original circle). Repeat with a third cloned circle. Draw two black, filled-in circles for eyes in the top circle. Note that you should draw one eye, thenclonethe eye and move it to the appropriate place.Please add two calls--one to
getMouse()and one toclose()--on yourGraphWinobject at the end of the program. It won't make a difference for you in IDLE, but it does make a difference for me when I run the programs automatically.There will be no IDLE output for this program.
- (16) Write a program that displays a canvas, requests users click
where a line, by its two endpoints, should be drawn on a canvas, and
then draws the line that connects those two points. (How can the
user specify the points of the line?) Display the directions to the
user on the canvas in a
Textobject, and update the directions as the user clicks.Example screen shots:
- (18) Create a program that draws "something significant", such
as a scene, a house, or a face, using the graphics library. Here
are some guidelines to follow:
- Your program should keep the drawn face/house/scene on the graphics window until the user clicks the mouse. Then the graphics window should close and your program should end.
- Use a variety of colors and several different types of shapes.
- For symmetrical features (such as eyes or ears), remember to
use the
clonemethod to make a copy of the original shape. Then draw the cloned shape in the window and move it to the desired location. - You may want to use the
setCoordsmethod of theGraphWinobject to adjust the coordinates of the window. (See the reference above for more information aboutsetCoords.)
Again, please add the calls to
getMouse()andclose()on yourGraphWinobject at the end of your program.Some previously created images to inspire you:

Screen Capture:
- To save the image you created, click on "Activities" in the upper left, and type "Screenshot". Click on the "Screenshot" program.
- Select "Grab the current window" option.
- Click the "Take Screenshot" button.
- Click on your image.
- Save the image in your
public_htmldirectory. Under the "Save in folder", click "Other", and then navigate to your home directory and then go topublic_html. Then click "Open". Now, "public_html" should show up in "Save in folder". Then, give the screenshot a descriptive name, e.g., "myHouse.png". Then, click "Save".
You've saved the image!
Reference Material for Graphics Programming
Objective: Creating a New Web Page (5)
It's time to revisit our web pages. Note that there are links in the directions for more info.
- Go into your
public_htmldirectory. - Copy your
index.htmlfile into a file calledlab2.html, still in thepublic_htmldirectory (i.e., not in a new directory) - If you haven't already, copy the screen capture image from your lab into
your
public_htmldirectory. - Open
lab2.htmlin jEdit. - Modify the Lab 2 web page to have an appropriate title, heading, and information about the image you created during this lab.
- View your web page in the browser. Recall the URL for this page. You may want to review Lab 0.
- Modify your Lab 2 web page to display the image you created.
- Clean up your Lab 2 page so that it only contains the image, a
link to your home page, and any appropriate text. (In other
words, get rid of the stuff that should only be on
your
index.htmlpage.) - Modify your
index.htmlpage to link to your Lab 2 web page. - Verify your web pages look correct in the browser. Make sure that the URL in the location bar starts with "cs.wlu.edu" (or similar) and not "file://" to verify that anyone on the Web can view your web pages.
Finishing up: What to turn in for this lab
- Submit your
lab2directory for grading by running the turnin.sh lab2 command. If you have every thing set up correctly, this will copy yourlab2directory into yourturnindirectory so that you and I (only) can see your submission. (If you run theturnin.shscript again, it will create a backup of the previous submission.) - Clean up: jEdit makes backup files and appends "~" to the name of your file. Delete any "~" files from your lab directory.
- Go into your
cs111directory. Create the printable lab assignment, using the command:createPrintableLab <labname> - View your file using the
evincecommand. If there are issues with your printout, go back and fix them. - Print the file using the
lprcommand introduced in the last lab.
Delete graphics.py and any image files
that are in your lab2 directory because they'll screw
up your printing.
Labs are due at the beginning of Friday's class. You should hand
in the printed copy at the beginning of class, and the electronic
version should be in the turnin
directory before 2:30 p.m. on Friday.
Ask well before the deadline if you need help turning in your assignment!
Grading (100 pts)
- Python programs: 95 pts; see above for breakdown
- Web page: 5 pts