These Guys? Wait, What? Really? Branching is a fundamental part of programming It means taking an...
-
Upload
florence-richards -
Category
Documents
-
view
218 -
download
0
description
Transcript of These Guys? Wait, What? Really? Branching is a fundamental part of programming It means taking an...
Branching In Python
Before We StartWhere Did Python Get It’s Name?
These Guys? Wait, What? Really?
Branching is a fundamental part of programming
It means taking an action based on decision The decision is dependent on a condition
Branching
if hungrytake a bite
if thirsty take a drink
If statements must have a condition The statement, “It’s 100 degrees outside”
◦ Is either true or false; we say, “evaluates to true or false”
◦ Helps us determine what to do next
Creating Conditions
Comparison Operators help us evaluate conditions to true or false
Comparison Operators
Operator Meaning Sample Condition
Evaluates To
== Equal to 5 == 5 True!= Not equal
to8 != 5 True
> Greater than
3 > 10 False
< Less than 5 < 8 True>= Greater
than or equal to
5 >= 10 False
<= Less than or equal to
5 <= 5 True
By indenting a line, it becomes a block A block is one or more consecutive lines
indented by the same amount Indenting sets lines off visually and logically
Using Indention
If (authenticated is True) :print(“Access Granted”)print(“Welcome!”)
If Followed By A Condition Followed By A Colon Followed By A Block If the condition evaluates to ‘True’, the
block is executed
Constructing Your Own If Statement
Helps make a choice based on a condition Do one thing if the condition is true, else do
something else if it is false
The Else Clause
password = input(“Please enter password”)
if (password == ‘secret’) :print(“Access Granted”)
else :print(“Access Denied”)
elif is short for ‘else if’; programmers are lazy and don’t want to type ‘else if’ when they can type ‘elif’
Allows the programmer to create a sequence of conditions to be evaluated
Introducing elif
More on elifif (testScore >= 90) :
grade = “A”
elif (testScore >= 80) :grade = “B”
elif (testScore >= 70) :grade = “C”
elif (testScore >= 60) :grade = “D”
else :grade = “F”
You can add more than one condition in the overall condition by adding ‘and’ and ‘or’
The overall condition is still evaluated to True or False
Combining “And” and “Or”
if (hungry and thirsty) :takeABite()takeADrink()
if (hungry or thirsty)stopAtMcDonalds()
Write a main function Ask the user for his/her age and store it in a
variable ‘age’ Convert age to an integer using the int()
function If your age is greater than 18, print, “You
can vote” If the age is less than 18 print, “You cannot vote”
In Class Exercises
Write a main function that asks the user for his/her age and store it in a variable ‘age’
Ask the user if he or she is a citizen. (yes or no) and store the result in a variable ‘citizen’
If your age is > 18 and you are a citizen, print, “You can vote”. Otherwise, print, “You cannot vote”
Exercises
Write a main function. Ask the user to enter the temperature; convert it to an integer
If the temperature is > 80, turn the air conditioner on and make sure the heater is off
If the temperature is between 65 and 79 turn the air conditioner off and make sure the heater is off
If the temperature is below 65 turn the heater on and make sure the air conditioner is off
Exercises