02 Python Continued - Lesson 2
02 Python Continued - Lesson 2
Introduction
Selection is a programming construct, which allows programs to take different pathways (execute different
lines of code) depending on conditions, and this is what we need to code our programs to make decisions.
Example 1
In this example, we have a variable called ‘number’ which contains the number 10. The IF statement checks
to see if the variable is equal to 10. In this example, the variable ‘number’ does indeed contain the number
10 (the condition evaluates to ‘True’), and so the print() statement (indented underneath) is executed (run),
outputting the string ‘The number equals 10’.
Example 2
In this example, the contents of the variable ‘number’ is now 5. This time, when the IF statement checks to
see if the variable is equal to 10, it finds that it doesn’t. This condition evaluates to ‘False’ as 5 does not
equal 10. As a result, the program executes (runs) the line of code indented underneath the ELSE statement,
outputting the string ‘The number doesn’t equal 10’.
Notice how after checking if the user entered 1, 2 or 3, there is a final else statement. It is always a good idea
to use this to return an error message in the event that the user did not enter an expected input.
Remember, if we are checking to see if a variable contains a number, we don’t need to enclose the number
in quotes. However, if we are checking if a variable contains a string, remember to place quotes around the
string, so that the condition can be correctly evaluated. Also, indentation is vital here, so make sure you
indent correctly!
PRIMM TASKS
1: Predict
Code
What do you think the code will do?
2: Run
3: Investigate
5: Make
Create a program in python for each point below and add a screenshot of your code in the boxes provided:
1) Write a program that asks the user for a number and says if they have entered a zero or not.
2) Write a program that asks the user for a letter and says if the letter is an ‘a’ or not.
3) Write a program that asks the user for a day of the week and states what you will be doing on that day (e.g. if
‘Monday’, “…I’ve got school!”
4) Write a program that asks the user for a number from 1-3. If it is a 1, the program will add 10 to it. If it is a 2, the
program will multiply it by 10. If it is a 3, it will divide the number by 10.
5) Write a program that ask the user for a month. The program will return the days that the month contains (30 days
has September, April, June and November, all the rest have 31, except for February, which has 28, but 29 on a leap
year).