Lesson 4 Python Selection1
Lesson 4 Python Selection1
www.computerscienceuk.com
Lesson Objectives
Lesson Objectives
• Remember how we program inputs/outputs/variables
• Remember what a variable’s data type means
• Remember how to tell the computer which data type a
variable is
• Learn how programs make decisions in Python
Lesson Outcomes
• Carrying out a number of programming tasks.
On your white boards, write a program which asks the user for two
numbers, adds then together, then displays the result.
www.computerscienceuk.com
Can you tell me the following…?
www.computerscienceuk.com
Dealing with numbers in Python
The need to convert “Data Types”
Twen
ty Nin
Num1
e
I’m telling
the computer
Twen 29
that this
variable
29 contains a
number not a ty Nin
word!
e
Variable Variable
Mr Int.
www.computerscienceuk.com
Once they have been converted we’re
ready to do maths!
Five Nine
Num1 Num2
5 9
Five Nine
Num1 Num2
14
Answer 5 + 9
14
Answer
1
4
www.computerscienceuk.com
Decisions (Selection)
www.computerscienceuk.com
Decisions in Python Programs
The Virtual Barman
www.computerscienceuk.com
The Program At Work…
How? www.computerscienceuk.com
How would we start?
• What would the barman do?
Ask the customer for their age
• What must our program do?
Ask the user for their age!
How?
Age = input(“What is your age?”)
www.computerscienceuk.com
Python and Integers?
age = input(“What is your age?”)
www.computerscienceuk.com
What next?
• If the real barman was told the
customers age what would the barman
do next?
MAKE A DECISION!
www.computerscienceuk.com
Selection
• If we want the computer to make a
decision based on our input we use
“selection”.
• Selection uses If – Else statements
Outcome 1
Outcome 2
www.computerscienceuk.com
Our Program
What is your age?
www.computerscienceuk.com
How are decisions programmed in
Python?
www.computerscienceuk.com
So how can we program our Barman
Program?
Write down your code on a whiteboard!
A Flowchart to Help!
START
A Solution:
Age?
Convert to
Integer
DISPLAY
IF Age “What can
> 17 I get you
young sir?”
DISPLAY
“Go home
boy!”
www.computerscienceuk.com
Comparison Operators
• In IF statements we will be looking to compare some data.
– We might want to see if a variable is equal to a number or a
piece of text.
– We might want to see if a variable contains a number greater
than another
– Or contains a number smaller than another.
www.computerscienceuk.com
How do these two pieces of code
differ?
www.computerscienceuk.com
Tasks
1. Create your own BARMAN program using
IF Statements