A2 Worksheet - Lucky Number Revisited
A2 Worksheet - Lucky Number Revisited
1 count = 10
2 while count >= 1:
3 print(count)
4 count = count-1
5 print("Lift off")
1 lucky = 13
2 guessed = False
3
while guessed == False:
4
print("Can you guess my lucky number?")
5
guess = int(input())
6
if guess != lucky:
7
print("Sorry, it's not", guess)
8
else:
9
print("Amazing, you guessed it")
10 print("Nice playing with you")from random
11 import randint
The program uses a flag variable called guessed to keep track of whether or not the
user has guessed the lucky number. The variable is initialised to False (line 2), but it is
never set to True, so the game never terminates.
Insert the following line in your program, wherever you think it should be.
This assignment sets guessed to True. It ‘raises the flag’ to indicate that the user has
guessed the number. This should cause the game to end when the condition in while is
Page 2
fulfilled.
Tip
Make sure that the guessed variable is set to True only in the case where the user
guesses the number.
Extend the program, so that it keeps track of how many times the user has attempted to
guess the lucky number.
At the end of the game, display this number to the user.
Example
Note: Use these numbers to test that your program works correctly. In general, the messages displayed
will depend on user input and will not always be the same.
Tip
Introduce a count variable to keep track of the number of user guesses.
Look at the count and question variables in the worked examples: they serve the
same purpose. They are assigned an initial value and modified in each iteration.
Page 3
guessed == False
This means that the game will continue for as long as guessed is False, i.e. the user still
hasn’t guessed the lucky number.
Extend this condition, to also check that the user has not exceeded a certain number
of guesses. For example, the user may only be allowed three guesses.
Tip
Your program uses the count variable to keep track of how many times the user has
attempted to guess the lucky number. Check this variable in the condition.
Look at how the count and question variables are checked in the while conditions of
the worked examples.
At the end of the game, the current program displays the number of attempts that the
user made at guessing the number.
Extend the program so that at the end of the game:
▢ If the user managed to guess the lucky number, the program displays the number
of guesses required (like it currently does)...
▢ … and otherwise, if the user’s guesses were incorrect, the program displays the
lucky number to the user
Example
Note: This is an example of the user’s successful final attempt. In general, the messages displayed will
depend on user input and will not always be the same.
Example
Note: This is an example of the user’s unsuccessful final attempt. In general, the messages displayed
Page 4
will depend on user input and will not always be the same.
Page 5
Explorer task . More information
In the current program, when a user’s guess is unsuccessful, they are only informed that
they didn’t guess the lucky number.
It would be great if the program provided some additional information, such as whether
the user should try a lucky number that is higher or lower than their current guess.
Example
Note: Use these numbers to test that your program works correctly. In general, the messages displayed
will depend on user input and will not always be the same.
Example
Note: Use these numbers to test that your program works correctly. In general, the messages displayed
will depend on user input and will not always be the same.
Tip
When a user attempts to guess the lucky number, there are now three possible
outcomes, so you will need multi-branch selection (if, elif, else).
Page 6
Explorer task . Randomness
In the current program, a specific lucky number is always selected.
lucky = 13
Modify this assignment, so that a random integer between 1 and 20 (inclusive) is selected
as a lucky number.
Tip
You will need to use the randint function, from the random module.
Resources are updated regularly — the latest version is available at: ncce.io/tcc.
This resource is licensed under the Open Government Licence, version 3. For more information on this
licence, see ncce.io/ogl.
Page 7