Conditional Logic Slides
Conditional Logic Slides
reflect_on_learning()
else:
take_break()
Recover From Errors
Errors happen, handle them gracefully.
Conditional logic
Control flow
Error handling
Conditional Logic: Add Some Logic
Conditional Logic: Add Some Logic
Find out whether something is true or false.
Boolean comparators
Conditional statements
Logical operators
Operator precedence
Boolean Comparators
True or False
True or True
False or False
False or True
not
not
not False
not True
Operator Precedence
Boolean comparators
Conditional statements
Logical operators
Operator precedence
Conditional Logic: Building Complex Expressions
Building Complex Expressions
Create branches
Control the Flow of Your Program
In this lesson you’ll be learning about:
if
else
elif
if … elif … else
if
if
if [condition]:
...
print("done")
if
if True:
print("Hello!")
if False:
print("Hello!")
print("done")
School Grading Example
else
else
if [condition]:
...
else:
...
elif
elif
if [condition]:
...
elif [condition]:
...
else:
...
elif
if [condition]:
...
elif [condition]:
...
elif [condition]:
...
else:
...
Control the Flow of Your Program
In this lesson you’ve learnt about:
if
else
elif
if … elif … else
Control Flow: Nested if Statements
Example: Evaluate the Winner
♻
Refactor Again!
♻♻
Challenge: Find the Factors of a Number
Control Flow: Break Out of the Pattern
Break Out of the Pattern
In this lesson you’ll cover:
sum_of_evens = 0
for n in range(101):
if n % 2 == 0:
sum_of_evens = sum_of_evens + n
print(sum_of_evens)
break
break
for n in range(4):
if n == 2:
break
print(n)
break
n = 0
while True:
print(n)
if n > 5:
break
n = n + 1
continue
continue
for n in range(4):
if n == 2:
print("there goes two")
continue
print(n)
continue
n = 0
while True:
print(n)
if n < 5:
n = n + 1
continue
else:
break
print("end of loop")
Break Out of the Pattern
In this lesson you’ve covered:
>>> if
...
if
^
SyntaxError: invalid syntax
ValueError
>>> int("hello")
Traceback (most recent call last):
...
ValueError: invalid literal for int() with base 10: 'not a number'
TypeError
>>> "1" + 2
Traceback (most recent call last):
...
TypeError: can only concatenate str (not "int") to str
NameError
>>> print(does_not_exist)
Traceback (most recent call last):
...
NameError: name 'does_not_exist' is not defined
ZeroDivisionError
>>> 1 / 0
Traceback (most recent call last):
...
ZeroDivisionError: division by zero
OverflowError
try:
number = int(input("Enter an integer: "))
except ValueError:
print("That was not an integer")
The try and except Keywords
try:
...
except [exception]:
...
Catching Di erent Exceptions
try:
# Do lots of hazardous things that might break
except:
print("Something bad happened!")
The Bare except Clause
Recover From Errors
In this lesson you’ve covered:
import random
def coin_flip():
"""Randomly return 'heads' or 'tails'."""
if random.randint(0, 1) == 0:
return "heads"
else:
return "tails"
Simulate
import random
def unfair_coin_flip(probability_of_tails):
if random.random() < probability_of_tails:
return "tails"
else:
return "heads"
Simulate and Calculate Probabilities
In this lesson you’ve implemented a program that:
Compared values
Used logical operators
Learned how to control the flow of your programs
Creating branches with if blocks
Using Loop keywords
Combining if with loops.
Learned how to handle exceptions
Additional Resources