Python 5
Python 5
Lesson five
Learning Objective
• To use a textual programming language to solve a variety of
computational problems.
Success Criteria
• To know how selection is used to make decisions in a computer program.
• To recognise and use comparison operators.
• To be able to use if, elif and else in Python programs.
Pyphone Passcode
• Start Python IDLE and type in the following code in the script mode window.
• Save and run your program.
# Screen Lock
print("Pyphone 360") Pyphone 360
passcode = input("Enter your passcode:") Enter your
passcode:
1234
if passcode == "1234":
print("Welcome - Pyphone unlocked") Welcome –
Pyphone unlocked
else:
print("Incorrect passcode")
Pyphone Passcode
# Screen Lock
print("Pyphone 360")
Pyphone 360
Answer: it prints the
What do these text “Pyphone 360” on
lines of code do? the screen.
Pyphone Passcode
passcode = input("Enter your passcode:")
if passcode == "1234":
print("Welcome - Pyphone unlocked")
else:
print("Incorrect passcode")
Selection
There are several things to remember when using selection in Python.
1.The keywords if and else must be in lower case (not UPPER CASE).
2.We always end each if line of code with a colon :
3.We always end each else line of code with a colon :
4.Notice that we use == instead of just a single =
(because == is a comparison operator, more about this later on…)
5.Notice how the lines of code after if and else are indented.
Indentation is very important in Python.
6.Python IDLE will automatically create indentation for you on the next line
when you use a colon correctly.
if passcode == "1234":
print("Welcome - Pyphone unlocked")
else:
print("Incorrect passcode")
Selection
Our last program only had two choices: the passcode was either correct or
incorrect. What if we wanted to have more than two choices?
•Solution: in Python we can use the elif keyword, which means else-if.
•Type in the following code in the script mode window, then save and run.
Key Terms
To program selection in Python we use the key words if, elif and else.
elif means ‘else if’. For every if statement, we can include any
number of elif statements. else can be thought of as meaning
‘none of the above’. For every if statement, we can include a single
else statement, or no else statements (else is optional).
Selection
Key Terms
Indentation is very important in Python. Each block of code after
an if, elif or else statement must be indented. There should always
be a colon : at the end of every line of code containing if, elif or
else statements. Python IDLE will then automatically indent the
next line of code when we use a colon correctly.
if something == True:
# stuff that happens if something is true
elif something else == True:
# stuff that happens if something else is true
else:
# stuff that happens if none are true
When Programs Go Wrong
Pause for Thought
Question: What would happen if your forgot to include a colon at
Key Terms
An error in a computer program is known as a bug. Try and
find out why. The process of finding and then fixing errors
in computer programs is often called debugging. Many IDEs
such as Python IDLE have inbuilt debugging tools.
Be the Best
• To join the British Army, you have to be 16 years or older, but no older
than 32 years old.
• Using your knowledge of selection,
comparative operators and Boolean operators,
write a Python program that will do the following:
1. Ask the user to enter their age.
2. Output whether or not they are eligible to
join the British Army.
• When you have finished:
Test your program several times to make sure that it works.
Then test someone else’s program.
Key Terms
To program selection in Python we use the key words if, elif and else.
elif means ‘else if’. For every if statement, we can include any
number of elif statements. else can be thought of as meaning
‘none of the above’. For every if statement, we can include a single
else statement, or no else statements (else is optional).
Let’s Bring It All Together
Key Terms
An error in a computer program is known as a bug.
The process of finding and then fixing errors in computer
programs is often called debugging. Many IDEs such as
Python IDLE have inbuilt debugging tools.
Key Terms
In Python, a single = is called an assignment operator. We use = to give
(assign) a value to a variable. For example: myAge = 15 means we
assign the value 15 to a variable called myAge.
A double == is called an equality operator. We use == to compare one
value
or condition with another value or condition to see if they are the same
(equivalent) or not. The == operator is often used with if statements.
The != is called an inequality operator.
Rate Your Progress
Red Light: you have understood some of the objective
and you will think about it some more later on, perhaps
asking a friend or teacher for help.
Green Light: you feel fully confident with this objective and
you understand it well.
Success Criteria:
•To know how selection is used to make decisions in a computer program.
•To understand why indentation is important in Python.
•To be able to use if, elif and else in Python programs.
Nailing It Down
• We have learned a lot today about selection and comparative
and Boolean operators in Python.
• Most computer programs make decisions based on two or more
conditions. The keywords IF and ELSE are used in selection and
are used in most programming languages.
• Knowledge-Based Systems (or ‘Expert’ systems) are sometimes used to
model real-life situations such as weather forecasting. These systems are
often very complex, and need a different way of processing than using IF
and ELSE to make simple comparisons.