0% found this document useful (0 votes)
25 views7 pages

A2 Worksheet - Lucky Number Revisited

Uploaded by

22pereirag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views7 pages

A2 Worksheet - Lucky Number Revisited

Uploaded by

22pereirag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Year 8 – Intro to Python programming Worksheet

Lesson 6 – Putting it all together

Lucky number revisited

Worked example from the last lesson . Countdown


The program below displays a sequence of numbers, starting from 10 and counting
down to 1. The count variable is used to keep track of the current number.

1 count = 10
2 while count >= 1:
3 print(count)
4 count = count-1
5 print("Lift off")

Worked example from the last lesson . Times tables


The program below asks the user a series of times tables practice questions and
provides feedback. The questions variable is used to keep track of how many
questions have been asked.

1 from random import randint


2 questions = 0
3 while questions < 3:
4 a = randint(2,12)
5 b = randint(2,12)
6 print(a, "times", b, "=")
7 answer = int(input())
8 product = a * b
9 if answer == product:
10 print("That is correct")

Page 1 Last updated: 20-04-21


11 else:
12 print("I am sorry")
13 print(a, "times", b, "is", product)
14 questions = questions + 1

Task . Guess the number


Open the Python program below (ncce.io/py-lucky-60) in your development
environment. It picks a specific ‘lucky number’ and keeps asking the user to guess it.

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.

Step 1: Ending the game

Insert the following line in your program, wherever you think it should be.

⚑ guessed = True # raise the flag

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.

Step 2: Counting guesses

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.

The program displays a prompt Can you guess my lucky number?


and waits for keyboard input.

The user types in a reply. 12

The program displays a message Sorry, it's not 12


that the user’s guess is incorrect.

The program displays a prompt Can you guess my lucky number?


and waits for keyboard input.

The user types in a reply. 13

The program displays a message Amazing, you guessed it!


that the user’s guess is correct.

The program displays the It took you 2 guesses


number of attempts. Nice playing with you

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.

Step 3: A limit to the guesses

This is the condition currently checked in the while statement:

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.

guessed == False and :

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.

Step 4: Final word

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.

The program displays a prompt Can you guess my lucky number?


and waits for keyboard input.

The user types in a reply. 13

The program displays a message Amazing, you guessed it!


that the user’s guess is correct, It took you 2 guesses
and another one with the number
of guesses that were required.

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.

The program displays a prompt Can you guess my lucky number?


and waits for keyboard input.

The user types in a reply. 12

The program displays a message Sorry, it's not 12


that the user’s guess is incorrect, My lucky number is 13
and another one with the actual
lucky number.

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.

The program displays a prompt Can you guess my lucky number?


and waits for keyboard input.

The user types in a reply. 12

The program displays a message My lucky number is larger than 12


with additional information about
the lucky number.

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.

The program displays a prompt Can you guess my lucky number?


and waits for keyboard input.

The user types in a reply. 16

The program displays a message My lucky number is smaller than 16


with additional information about
the lucky number.

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.

from random import randint


lucky = .

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

You might also like