0% found this document useful (0 votes)
18 views

Python Lesson 5 - Selection

Uploaded by

sy.shady86
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Python Lesson 5 - Selection

Uploaded by

sy.shady86
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Selection

Lesson 5


• A variable name can only be one word

• A variable name must start with a letter or the underscore character

• A variable name cannot start with a number

• A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )

• Variable names are case-sensitive (age, Age and AGE are three different variables)

 A variable can have a short name (like x and y) OR

 A more descriptive name (age, carName, total_volume)



• Start Python and type in the following code in the script mode window.
• Run your program.

# Screen Lock Pyphone 360


print("Pyphone 360")
Enter your
passcode = input("Enter your passcode:") passcode:
1234

if passcode == "1234": Welcome – Pyphone


unlocked
print("Welcome - Pyphone unlocked")
else:
print("Incorrect passcode")
# Screen Lock
print("Pyphone 360")

Pyphone 360
Answer: it prints the
What does this text “Pyphone 360”
line of code do? on the screen.
passcode = input("Enter your passcode:")

Answer: it prints the


Pyphone 360 text “Enter your
passcode:” on the
What does this Enter your screen and waits for
line of code do? passcode: user input.
if passcode == "1234":
print("Welcome - Pyphone unlocked")

Pyphone 360 Answer: if the user enters the code


1234 then it prints the text “Welcome
What do these
Enter passcode: - Pyphone unlocked” and the Pyphone
lines of code do? 1234 will be unlocked.

Welcome – Pyphone
unlocked
else:
print("Incorrect passcode")

Pyphone 360 Answer: if the user enters the


wrong code then it prints the text
What do these Enter passcode: “Incorrect passcode” and the
lines of code do? 5678 Pyphone will remain locked.

Incorrect passcode
• This Python program is an example of selection.

• Selection is how computer programs make decisions.

• In Python, as with many other programming languages, we use the keywords if and else.

• It works like this:


IF something is true, THEN
do this.
ELSE (if the something is NOT true) THEN
do that.
if passcode == "1234":
print("Welcome - Pyphone unlocked")
else:
print("Incorrect passcode")
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 comparative 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")
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 and run.

# Grades Calculator >= means


score = int(input("Enter your score:")) greater than or
equal to.
if score >= 80:
print("Great score, well done!")
elif score >= 50:
print("Not bad, could do better.")
else:
print("Oh dear!")
• In Python, for every if, we can have one or more elif, and one else.
• else is optional - we only use it if we need to.
• We can improve our last program by using more elifs. Try this yourself:

score = int(input("Enter your score:")) Now try adding some more


if score >= 80: elif lines for grades D (>=50),
E (>=40) and F (>=30).
print("Grade A")
elif score >= 70:
print("Grade B")
elif score >= 60:
print("Grade C")
else:
print("Oh dear!")
# Grades Calculator Solution
score = int(input("Enter your score:"))
if score >= 80:
print("Grade A")
elif score >= 70: Run your program several
times and test that it works for
print("Grade B") scores between 1 and 100.
elif score >= 60:
print("Grade C")
elif score >= 50:
print("Grade D")
elif score >= 40:
print("Grade E")
elif score >= 30:
print("Grade F")
else:
print("Oh dear!")
Pause for Thought
What would happen if your forgot to include a colon at the end
of an if statement? Answer: it would produce a syntax error.
Try it now and see for yourself.
Do some research to find out what a syntax error is.
How does Python IDLE help you find and fix syntax errors?

>>> if something == True


SyntaxError: invalid syntax

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.
Selection
Selection is when computers make decisions.
A computer can be programmed to make a selection between two or more
choices, depending on the condition (the question).

If, elif and else

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).
Indentation
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
Success Criteria

You might also like