Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
25 views

Python 5

This document provides an introduction to selection in Python programming. It explains how to use if, elif, and else statements to make decisions in code through comparisons. Examples are given to check a passcode and calculate grades based on scores. Key terms around selection, errors, and debugging are also defined.

Uploaded by

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

Python 5

This document provides an introduction to selection in Python programming. It explains how to use if, elif, and else statements to make decisions in code through comparisons. Examples are given to check a passcode and calculate grades based on scores. Key terms around selection, errors, and debugging are also defined.

Uploaded by

ali.wessam4444
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Computing: Introduction to Python

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:")

Answer: it prints the


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

Pyphone 360 Answer: if the user


enters the code 1234
What do these Enter passcode: then the Pyphone will
lines of code do? 1234 be unlocked.
Welcome –
Pyphone unlocked
Pyphone Passcode
else:
print("Incorrect passcode")

Answer: if the user


Pyphone 360 enters the wrong
What do these code then the
Enter passcode:
lines of code do? Pyphone will remain
5678
locked.
Incorrect passcode
Selection
• 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")
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.

# 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!")
Selection
• 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:"))


if score >= 80:
print("Grade A") Now try adding some more elif
lines for grades D (>=50),
elif score >= 70: E (>=40) and F (>=30).
print("Grade B")
elif score >= 60:
print("Grade C")
else:
print("Oh dear!")
# Grades Calculator Solution
title
score = int(input("Enter your score:"))
if score >= 80:
print("Grade A")
Run your program several times
elif score >= 70: and test that it works for scores
print("Grade B") 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!")
Selection
Key Terms
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).

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

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.
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.

I was born in 1989.


Can I join the army?
Be The Best
Alternative solution without using a Boolean operator.
# Be The Best
age = int(input("Please enter your age:"))

if age >= 16:


if age <= 32:
print("Yes, you can join the Army.")
else:
print("Sorry, you can't join the Army.")
else:
print("Sorry, you can't join the Army.")

Which version do you prefer?


Cinema Challenge
According to the British Board of Film Classification, movies shown at the
cinema should have one of the following age certificates:
•U (suitable for all, ages 4 and over)
•PG (parental guidance)
•12A (suitable only for 12 years and over)
•12 (suitable only for 12 years and over)
•15 (suitable only for 15 years and over)
•18 (suitable for adults only)
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 a list of film age certificates which the user can watch
(i.e. U, PG, 12A, 12, 15 or 18).
When you have finished:
Test your program several times to make sure that it works.
Then test someone else’s program.
Cinema Challenge
Possible solution:
# Cinema Selection
# Get age of user
age = int(input("How old are you?"))
# Selection
if age >= 18:
print("U, PG, 12, 12A, 15, 18")
elif age >= 15 and age < 18:
print("U, PG, 12, 12A, 15")
elif age >= 12 and age < 15:
print("U, PG, 12, 12A")
elif age >= 4:
print("U, PG")
else:
print("Sorry, not old enough!")
Let’s Bring It All Together
Key Terms
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).

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.

Amber Light: you have understood most of the objective


and you are happy with your progress.

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.

Check out the following interesting links


to try out an expert system for yourself.
Can you find out how these are coded?
http://20q.net/
https://www.nhsdirect.wales.nhs.uk/SelfAssessments/

You might also like