These Guys? Wait, What? Really? Branching is a fundamental part of programming It means taking an...

14
Branching In Python

description

 Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent on a condition if hungry take a bite if thirsty take a drink

Transcript of These Guys? Wait, What? Really? Branching is a fundamental part of programming It means taking an...

Page 1: These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.

Branching In Python

Page 2: These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.

Before We StartWhere Did Python Get It’s Name?

These Guys? Wait, What? Really?

Page 3: These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.

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

Page 4: These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.

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

Page 5: These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.

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

Page 6: These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.

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!”)

Page 7: These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.

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

Page 8: These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.

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”)

Page 9: These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.

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

Page 10: These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.

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”

Page 11: These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.

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()

Page 12: These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.

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

Page 13: These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.

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

Page 14: These Guys? Wait, What? Really?  Branching is a fundamental part of programming  It means taking an action based on decision  The decision is dependent.

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