# 111 lab3

Computer Science 111 – Fundamentals of Programming IProgramming Project 3
Due date: 11:59PM Friday, 4 October

Warmup

Enter the following statements in the IDLE shell:

import random

dir(random)

help(random.randint)

random.randint(1, 10)

for x in range(10): print(random.randint(1, 10))

import math help(math.log) math.log(16, 2) math.log(100, 2)

for exponent in range(10):
powerOfTwo = 2 ** exponent
logBaseTwo = int(math.log(powerOfTwo, 2)) print(“%-5d%d” % (powerOfTwo, logBaseTwo))

To Turn in

Create a project3 directory in your home directory. Copy the file game.py from the webpage to this directory. You will modify this file and turn it in as the first part of your lab work for this week. Complete the following exercises.

1. Run the game program in IDLE and play the game a couple of times. If you make correct use of the hints, you should be able to guess the computer’s number in no more thanint(log2(100)) + 1 guesses. Why? Modify the game program so that the computer ends the game if the number of guesses exceeds the maximum necessary.
2. Copy the game.py file to a new file named game2.py. Modify the guess the number program in the new file so that the computer guesses the number (still between 1 and 100) and the user provides the hints. Useful, simple hints would be small and large. The user should say correct when the computer guesses right, terminating the loop and reporting the count of guesses. Your program should contain no unnecessary code; for example, since the human is thinking of the random number, there is no need to import the random library.

3. The German mathematician Gottfried Leibniz developed the following method to approximate the value of :

pie/4 = 1 – 1/3 + 1/5 – 1/7 + . . .

Write a program (in the file leibniz.py) that allows the user to specify the number of iterations used in this approximation and displays the resulting estimate of  and Python’s own math.pi.

For five points extra credit, write a program leibniz2.py that asks the user for a tolerance, keeps looping until the estimate gets within that tolerance of math.pi, and reports the number of iterations at the end.

For another five points extra credit, write a program leibniz3.py that does the same things as leibniz2.py but with an improved formula for fewer iterations (GIYF). In the header comments of this program, report where you found the formula, and how much of an improvement it gives over the original formula in terms of iterations required.

Drop your project folder into the course turnin directory.