$29
Learning objectives:
Introduce pair programming.
Use Python graphics.
BE SURE THAT THE NAMES OF BOTH STUDENTS ARE IN THE FILE!
Show your code to an instructor as you complete it. You must demonstrate your code and be advised on your code to receive credit for having completed each assignment.
Download graphics.py. All programs in this lab need it.
Download the program Lab5.py
1. Cup conversion using graphics.
To get input from the user within a GraphWin object, you need to use the author’s Entry object. Once you have an Entry object, you can call a method to get the text, getText(), from the object. This method, unlike getMouse(), will not wait on interaction from the user. Thus to use the method you must call the getMouse() method before calling getText() to have the window wait until the user clicks to get the value typed. A call to getText(), returns the string representation of whatever the user types in the entry box. If you want this value to be a number, you must convert it using the eval() function. Modify the function cupConverter()that creates a simple entry box to implement your cup converter. Read the comments to understand the code.
For your function, ask the user to input a number of cups, then display this number as milliliters. A cup equals 8 ounces. An ounce equals 29.57 ml. All input/output should be in a graphics window.
Show your answers to the instructor. Switch driver and navigator.
2. Target. (p. 118, #2, Slightly modified.)
An archery target consists of a central circle of yellow surrounded by concentric rings of red, blue, black and white. Each ring has the same “width,” which is the same as the radius of the yellow circle. Write a function, target(), that draws such a target. The target should appear in the center of the window. Adjust the size of the target so that it just fits in the window. Include an instruction to click to close the window.
Hint: Objects drawn later will appear on top of objects drawn earlier.
Show your answers to the instructor. Switch driver and navigator.
3. Triangle Information. (p. 119, #10)
Write a function, triangle.py, that displays information about a triangle drawn by the user.
Input: Three mouse clicks
Output: Draw the triangle. Display the perimeter and area of the triangle in the graphics window.
Formulas:
To calculate line length:
dx = x2 – x1
dy = y2 – y1
Use Euclidean distance to calculate:
To calculate area:
and ,
where a, b, and c are the lengths of the sides.
Show your answers to the instructor. Switch driver and navigator.
4. Color Shape
Write a function, colorShape(), that allows the user to color a shape based on red, green and blue values. A shell has already been started which draws a circle into a window and adds the labels for the entry boxes. Add code to create three entry boxes that allow a user to type amounts of red, green and blue between 0-255. When the user clicks on the window, the shape should change color to the color identified using the rgb amounts. The author’s graphics package includes a function color_rgb(redAmt, greenAmt, blueAmt) that is discussed on page 113. Write the code so it allows the user to enter 5 different color combinations before terminating. Make sure the user can click another time to close the window.
5. Not another series
Write a function, anotherSeries(), that allows the user to tell the number of terms and then adds that number of terms together from the following series. Display the result.
2 + 4 + 6 + 2 + 4 + 6 +….
6. Not another series…for the other partner
Write a function, anotherSeriesPartner(), that allows the user to tell the number of terms and then muliplies that number of terms together from the following series. Display the result.
1/3 * 3/6 * 5/12 * 7/24 *….
When your lab is complete, un-comment the function calls and upload Lab5.py to both student accounts. BE SURE THAT BOTH NAMES ARE IN THE FILE!
Delete any files that you have put on the desktop. Empty the trash. Log off the computer.