A2 Worksheet - L4
A2 Worksheet - L4
A2 Worksheet - L4
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).
used = instead of == in the condition for if, to check if two values are equal
used quotes around the name of a variable
1 import random
2 people = random.randint(1,10)
3 print(people, "people in space right now")
Step 1
Open Copy this pice of code into REPLIT.COM and extend it, so that it asks the user to
guess the number of people currently in space.
Example
Note: The number of people in space is retrieved from an online service through the people function. It
is not always the same and the numbers shown here are just an example.
The program displays a prompt How many people do you think are in
and waits for keyboard input. space right now?
The user types in a reply. 5
Tip
Don’t delete or modify any of the existing program statements, as you will need them.
Simply insert any additional instructions.
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.
Tip
Before you proceed to the next step, make sure that you run your program, to verify
that there are no errors.
Step 2
Extend the program so that it compares the number of people in space with the user’s
guess and displays an appropriate message.
Example
Note: The number of people in space is retrieved from an online service through the people function. It
is not always the same and the numbers shown here are just an example.
The program displays a prompt How many people do you think are in
and waits for keyboard input. space right now?
The user types in a reply. 8
Example
Note: The number of people in space is retrieved from an online service through the people function. It
is not always the same and the numbers shown here are just an example.
The program displays a prompt How many people do you think are in
and waits for keyboard input. space right now?
The user types in a reply. 5
Example
Note: The number of people in space is retrieved from an online service through the people function. It
is not always the same and the numbers shown here are just an example.
The program displays a prompt How many people do you think are in
and waits for keyboard input. space right now?
The user types in a reply. 9
Tip
There are three branches, so use multi-branch selection: if, elif, else.
Tip
Use < or > to compare if a value is less than or greater than another.
Explorer task . Temperature
Write this code into REPLIT.COM and check that it works.
import random
temperature = random.random int(5,30)
print("Where do you live?")
location = input()
temp = temperature(location)
Step 1
Extend the program so that it displays an appropriate message depending on the range
that the current temperature is in.
Example
Note: The current temperature is retrieved from an online service. It is not always the same and the
numbers shown here are just an example.