You are on page 1of 2

UE 104 Introduction to Computer Science 1

PW 2: Refining the algorithm/code


Goal

● Understanding and using the refining processes.

Exercice 1: Game “guess the number”

The game is played by two players. The first player chooses a number between 1 and 999. The second
player must find it in a minimum number of tries. With each proposition, the first player indicates
whether the proposed number is greater or less than the number to be found. At the end of the
game, the number of tries is given.

Note: It is assumed that there is a function which allows us to obtain a random number between 1
and 999. In Python, this function is called randint (for "random integer") and is used as follows:

number = randint(1, 999) # random number

The randint function is defined in the random module. To access it, add (at the start of the file):

from random import randint

1. User plays. Write a program in which the machine chooses a number and makes the user
guess it. Of course, to write this program you should follow the refining method.
2. Machine plays. Write a program in which the user chooses a number and the machine must
find it. For each number offered, the user indicates whether it is too small ('s' or 'S'), too large
('b' or 'B') or found ('f', 'F'). Of course, to write this program we will apply the refining method.
3. The full program. Write the game program that gives the user the choice to play or to let the
machine play.
4. We can start over. Complete the previous program so that the user will be asked to play a new
game when the previous one is finished.


UE 104 Introduction to Computer Science 1

Exercice 2:

Let's go back to the written program and consider some changes.

1. How does the program behave if the user cheats by changing the number to find during the
game?
2. Can we guarantee that the program ends and the user gives a valid indication (big, small,
find)?
3. Allow the user to use the symbols ">" to mean too large, "<" to mean too small and "=" to
mean found.
4. Instead of looking for the number between 1 and 999, we want to look for it between 1 and
9999. What modifications must be made? Normally there should only be one line to change!
5. The user should be able to abandon the game by answering 0 when the program waits for its
response. The program should clearly state that the user has quit the game.
6. When the user requests to abort, the program must ask for confirmation before the abort is
effective.
7. If the player is cheating, the program would have to ask the user for the number they chose
and tell them when they made a mistake while evaluating the machine's suggestions.

You might also like