Python
Programming
Lesson 5 - Selection
Task 1 - Traffic Light
Create a simple program that checks whether the user has entered Red, Amber or Green when asked what the colour of the traffic light is. If it is Green it should tell them to “Go,” otherwise tell them to stop. Use the trinket below to program the solution.
1. Use the input command to ask the user to enter the colour of the traffic light, the response should be store in a variable called colour.
2. Add the following code under your input:
if colour == "Green":
print("Go")
else:
print("Stop")
Now Try
The program does not consider if the program is Amber as it should say “Get Ready”. You can check a 2nd condition using elif and checking if the colour entered is Amber. Look at the code below that uses an IF and checks a 2nd condition.
if number == 1:
print("You entered number 1")
elif number == 2:
print("You entered number 2")
else:
print("You entered number 3")
Notice instead of ELSE after the first true condition it says ELIF and then has another condition.
As there are three possible outcomes, 1,2 or 3 you can check if it is 1, check if it is 2 and if it is neither of them it has to be 3.
IF statements are structured as follows:
IF CONDITION THEN
DO IF TRUE
ELSE
DO IF FALSE
ENDIF
What the IF statement does:
CONDITION – this checks what you are looking for, in this case it checks to see if the colour entered is equal to Green
DO IF TRUE – if the condition is met, it will display a print message saying Go
DO IF FALSE – if the condition is not met it says Stop
Task 2 - Which number is bigger?
You need to create a program that allows the user to enter two numbers. The program will then need to check which number is bigger and tell the user in a print message. Create the program using the success criteria and do it in the trinket below.
Use the success criteria below to help you:
Use input to ask the user to “Enter the first number” and store it in a variable called firstNum
Use input to ask the user to “Enter the second number” and store it in a variable called secondNum
A IF statement that checks to see if the first number entered is bigger than the second number:
If it is, it should display a message saying the first number is the biggest
If it isn’t, it should display a message saying the second number is the biggest.
An example of how the program should work is shown on the right.
Extension
At the moment if both number are the same it will say “The second number is the biggest”.
Improve your program so that it will say if both numbers are the same, like shown below:
You will need to:
Add an ELSEIF to see if the second number is bigger than the first number.
If it is then display a suitable message.
If it isn’t (ELSE) say both numbers are the same
Evidence point
Open the Assignment in Google Classroom called Student Evidence - L5 - Selection.
Take a screenshot of your code for:
Task 1 - Traffic Light
Task 2 - Which number is bigger?
For each program click on Share and choose Link. Copy the link and paste it into the document, this means you can come back to it in future lessons.