CPSC120A
Fundamentals of Computer Science I

Lab 13

While Loops

Use the command line to create a new directory called lab13 in your labs directory. Make sure all of the .py files that you create for this activity are in that directory.

Leibniz Formula Approximation of pi

Create a Python function called approximate_pi in a file called pi.py. The function should return an approximation of pi using the Leibniz formula. The function should have one parameter, a positive integer, for the number of terms to sum when computing the approximation to pi. The Leibniz formula is:

π / 4 = 1 / 1 − 1 / 3 + 1 / 5 − 1 / 7 + 1 / 9…

For example, if the number of terms is 3, then the function should return 3. 466666666666667 because:

π / 4 = 1 / 1 − 1 / 3 + 1 / 5
π / 4 = 0. 8666666666666667
π = 3. 466666666666667

Be sure to create multiple test cases that print the the actual and expected output for different inputs. The test cases should have coverage for the minimum number of times the loop can run and for more times.

Average

Create a Python program in a file called average.py. The program should repeatedly prompt the user to enter a non-negative integer. When the user enters a negative integer, the program should print the average of all non-negative integers previously entered. For example one run of this program would look like:

Enter a non-negative integer (negative to quit): 1
Enter a non-negative integer (negative to quit): 2
Enter a non-negative integer (negative to quit): 3
Enter a non-negative integer (negative to quit): -1
The average of the integers you entered is: 2.0

Submission

Please show your source code and run your test cases for the instructor or lab assistant. Only a program that has perfect style, thorough test cases, and flawless functionality will be accepted as complete.