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

Python L3 Selection

This document discusses selection statements in Python programs using if, else, and elif. It provides examples of using comparison operators and if statements to check conditions and execute different code blocks based on the evaluation. It also introduces the elif statement to allow for additional options beyond a simple if/else.

Uploaded by

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

Python L3 Selection

This document discusses selection statements in Python programs using if, else, and elif. It provides examples of using comparison operators and if statements to check conditions and execute different code blocks based on the evaluation. It also introduces the elif statement to allow for additional options beyond a simple if/else.

Uploaded by

lucybaik24
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Selection: Non-linear

programs
Introduction to Python
Learning objectives
• Use selection statements if, else and elif in a
program
• Learn how to use different comparison operators
• Use indentation correctly to define a block of code
Introduction to Python
Selection

What exactly does this program


do?
#Password Checker
print("Welcome to PGO Security Systems")
print("*******************************")
password = input("Enter your password: ")
if password == "abcd1234":
print("Access Granted")
else:
print("Access Denied")
print("Press ENTER to exit the program")
Introduction to Python
Selection

Indentation!
• Python requires indentation as part of the syntax
• Indentation signifies the start and end of a block of
code
• Programs will not run without correct indentation
if password == "abcd1234":
print("Access Granted")
else:
print("Access Denied")

print("Press ENTER to exit the program")


Introduction to Python
Selection

Using an IF statement
• An IF Statement uses Selection
• IF forces a decision
• If condition Then do this Else do that

If nobody saw me do it Then


might get away with it
Else
busted
Introduction to Python
Selection

IF syntax
age = int(input("Enter age: "))
if age >= 18:
print ("Adult")
else:
print ("Child")
Introduction to Python
Selection

Comparison operators

Operator Meaning Example Evaluates to

== equal to 7==7 True

!= not equal to 6!=7 True

> Greater than 7>6 True

< Less than 5<8 True

>= Greater than or equal to 6>=8 False

<= Less than or equal to 7<=7 True


Introduction to Python
Selection

Who uses IF statements?


• Thinking of a mobile
phone, for example,
where might an IF
Statement appear in
its coding?
Introduction to Python
Selection

Write a program
• Write a simple program that might be used inside a
police speed camera
The Requirements are:
– It must accept the driver’s
speed
– IF the speed is over 70mph, a
message “Issue Fine” should
appear on the speed gun
– Otherwise it should display
“No Action”
Introduction to Python
Selection

The ELIF statement


• If gives you two
options if grade >= 80:
• ELIF stands for print("Distinction")
Else, If and gives elif grade >= 70:
you more options print("Merit")
• You can use it as elif grade >= 60:
many times as you print("Pass")
like else:
print("Fail")
Introduction to Python
Selection

Using the ELIF statement


• Add an ELIF
statement to the
Speeding program
• Issue a warning
between 70 and
75mph
• Only issue a fine for
75mph and over

You might also like