Starting from:
$35

$29

Laboratory 4 Graphics and Accumulation Practice Solution

Learning objectives:

 
Use Python graphics.

 
Use the author-supplied graphics package.

 
Practice accumulating sequence.




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 Lab4.py




1. Playing with Graphics.

In lab4.py there is a function,squares(), that draws circles in locations clicked by the user. Modify the squares() function to do Chapter 4, Programming Exercise 1 (p. 118), provided in the comments for the function. Make sure that the center of the square is at the point where the user clicks. Finished product should have a total of 6 squares in the window (the original square plus the 5 more clicked by user.)




2. Building a rectangle from its corners.

Add the function rectangle() to your code and do Chapter 4, Programming Exercise 9 (p. 119). Display the numerical output in the graphics window – don't use print. Ask the user to click to end the program, and be sure to close the window at the end. Make the window 400 by 400.




3. Drawing a circle from its center and radius.

Add a function called circle() to draw a circle. The first mouse click determines the center of the circle. The second mouse click determines a point on its circumference. Use the Euclidean distance formula to determine the length of the radius:

 

Ask the user to click to end the program, and close the window at the end.




4. Approximating pi a different way

Write a function, pi2(), to approximate the value of pi by summing the terms of this series:

4/1 – 4/3 + 4/ 5 – 4/7 + 4/9 – 4/11 + …. The program should prompt the user for n, the number of terms to sum, and then output the sum of the first n terms of this series. Have your function subtract from the value of math.pi to see how accurate it is.




5. Last week’s “bonus” pi is now mandatory…

Last week you were asked to calculate pi using Wallis formula. Most of you did not complete this problem. This week the problem is mandatory. A helper problem, to create a sequence was also discussed in last week’s lab. My recommendation: if you didn’t get the sequence problem solved, start with that. If you did get sequence working correctly, look at that code to solve pi. The sequence problem is the solution for the denominator of the Wallis formula pi sequence which is similar to the numerator. Here’s the pi problem restated:




Write a function pi() that approximates the value of using the Wallis formula:

π 2 * 2 * 4 * 4 * 6 * 6 * 8

2 1 3 3 5 5 7 7




The program should prompt the user for n, the number of terms in the series, compute the product of the n terms, and output the resulting approximation of pi.
















When your lab is complete, un-comment the function calls and upload Lab4.py to your student account.

 

Delete any files that you have put on the desktop. Empty the trash. Log off the computer.

More products