A2 Worksheet - Practise Using Selection
A2 Worksheet - Practise Using Selection
A2 Worksheet - Practise Using Selection
Lesson 3 – At a crossroads
Save a copy
This is an example of the Python program that you have developed so far: it prompts the user for their
name and reserves a special greeting for anyone named Elizabeth.
Syntax checklist
If you encounter an error message, read it and try to fix the problem. Use the list below to check for
common errors (and tick ✓ if you find yours).
Tip: In every task, the problem statement includes sample interactions between the user and the
program. Use the values provided in these examples to test your program.
Example
Note: The result displayed depends on user input, so it will not always be the same.
The program displays the result. Star Wars is not too bad
Example
Note: The result displayed depends on user input, so it will not always be the same.
Step 1
Open this incomplete program (ncce.io/py-critic-30) in your development environment:
Step 2
Complete line 3 with the condition that your program will need to check.
Tip: Use == to check if two values are equal, or != to check if two values are different.
Step 3 Step 4
Indent any line(s) of code that you believe should Once you manage to run your program
be indented. successfully, test it.
1 lucky = 13
2 print("My lucky number is", lucky)
Step 1
Extend this program into a number guessing game. The program should ask the user to guess the
lucky number, and then it should display a message, depending on whether or not the user guessed 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.
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 variable called guess, to refer to the number entered by the user.
Tip
Don’t forget that the user’s guess should be an integer. You will need to use int to convert user input
from the keyboard to an integer.
Tip
Use == to check if two values are equal and != to check if they are different. Do not confuse == with
=, which is used in assignments.
Step 2
Extend the program that you created in the previous task so that, regardless of the outcome, this
message is displayed at the end of the game:
Step 3: Checklist
Perform each of the tests below (and tick ✓ the boxes when you have finished them).
When the user guesses the lucky number, does the program display a message that the guess
is correct?
When the user fails to guess the lucky number, does the program display a message that the
guess is incorrect?
Does the program display a message that reveals the magic number only when the user’s
guess is incorrect?
Does the program always display a goodbye message to the user, regardless of the outcome
of the game?
Example
Note: Use these numbers to test that your program works correctly. In general, the result displayed will depend on user
input.
Example
Note: Use these numbers to test that your program works correctly. In general, the result displayed will depend on user
input.
Step 1
Write your program, run it, and test it. Use the code from the worked example and the previous tasks
as points of reference.
This resource is available online at ncce.io/prg4-3-a2-w. Resources are updated regularly — please check that you are using
the latest version.
This resource is licensed under the Open Government Licence, version 3. For more information on this licence, see
ncce.io/ogl.