Skip to content
Snippets Groups Projects
Commit 07bb8e8c authored by Cole Nelson's avatar Cole Nelson
Browse files

lec08

parent b8720332
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
# CS220: Lecture 08
## Learning Objectives
After this lecture you will be able to...
- write conditional statements
- using conditional execution ( if )
- using alternate execution (if/else)
- using chained conditionals (if/elif/elif/…/else)
- identify code blocks (indentation layers) in a program
- count the number of blocks in a segment of code
- determine the output of code with conditional statements
%% Cell type:markdown id: tags:
## Warmup
Let's trace the following code (Practice Problem 3) [link](https://pythontutor.com/visualize.html#code=def%20f%28%29%3A%0A%20%20%20%20x%20%3D%201%0A%20%20%20%20print%28x%29%0A%20%20%20%20g%28%29%0A%0Adef%20g%28%29%3A%0A%20%20%20%20x%20%3D%202%0A%20%20%20%20print%28x%29%0A%20%20%20%20h%28%29%0A%0Adef%20h%28%29%3A%0A%20%20%20%20x%20%3D%203%0A%20%20%20%20print%28x%29%0A%0Af%28%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)...
%% Cell type:code id: tags:
``` python
def f():
x = 1
print(x)
g()
def g():
x = 2
print(x)
h()
def h():
x = 3
print(x)
f()
```
%% Output
1
2
3
%% Cell type:markdown id: tags:
Then let's make some changes to it! [link](https://pythontutor.com/visualize.html#code=x%20%3D%20100%0A%0Adef%20f%28%29%3A%0A%20%20%20%20x%20%3D%201%0A%20%20%20%20print%28x%29%0A%20%20%20%20g%28%29%0A%0Adef%20g%28%29%3A%0A%20%20%20%20x%20%3D%202%0A%20%20%20%20print%28x%29%0A%20%20%20%20h%28%29%0A%0Adef%20h%28%29%3A%0A%20%20%20%20print%28x%29%0A%0Af%28%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)
%% Cell type:code id: tags:
``` python
x = 100
def f():
x = 1
print(x)
g()
def g():
x = 2
print(x)
h()
def h():
print(x)
f()
```
%% Output
1
2
100
%% Cell type:markdown id: tags:
And finally [link](https://pythontutor.com/visualize.html#code=x%20%3D%20100%0A%0Adef%20f%28%29%3A%0A%20%20%20%20x%20%3D%201%0A%20%20%20%20print%28x%29%0A%20%20%20%20g%28%29%0A%0Adef%20g%28%29%3A%0A%20%20%20%20global%20x%0A%20%20%20%20x%20%3D%202%0A%20%20%20%20print%28x%29%0A%20%20%20%20h%28%29%0A%0Adef%20h%28%29%3A%0A%20%20%20%20print%28x%29%0A%0Af%28%29&cumulative=false&curInstr=0&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)...
%% Cell type:code id: tags:
``` python
x = 100
def f():
x = 1
print(x)
g()
def g():
global x
x = 2
print(x)
h()
def h():
print(x)
f()
```
%% Output
1
2
2
%% Cell type:markdown id: tags:
## Today: Conditional Statements
%% Cell type:markdown id: tags:
Bob runs a community bakery. Every day, he recieves a shipment of dough balls, each of which bakes 4 loaves of bread, and he evenly splits the bread among his customers. Some days he may recieve 10 customers, 20 customers, and some days none at all! Below is the code that tells Bob how much bread he gave to each customer.
Identify and correct the errors in this code.
%% Cell type:code id: tags:
``` python
balls_of_dough = int(input("How many balls of dough did we recieve? "))
num_customers = int(input("How many customers were there? "))
bread_baked = balls_of_dough * 4
if num_customers == 0:
print("Sorry! No one showed up. All the bread went to waste.")
else:
bread_per_customer = bread_baked / num_customers
print("Each customer gets", round(bread_per_customer, 2), "loaves of bread.")
```
%% Output
How many balls of dough did we recieve? 3
How many customers were there? 0
Sorry! No one showed up. All the bread went to waste.
%% Cell type:markdown id: tags:
## Age Categorizer
Assume `age` is an int parameter of `categorize_age`. Categorize the person's age.
All the following bounds are inclusive (meaning you should include both ends)
- Return "Baby" if between ages of 0 and 1
- Return "Toddler" if between ages of 2 and 4
- Return "Child" if between ages of 5 and 17
- Return "Adult" if between ages of 18 and 64
- Return "Senior" if between ages of 65 and 125
- Return "Not a valid age" for all other ages.
%% Cell type:code id: tags:
``` python
def categorize_age(age):
if 0 <= age <= 1:
return "Baby"
elif 2 <= age <= 4:
return "Toddler"
elif 5 <= age <= 17:
return "Child"
elif 18 <= age <= 64:
return "Adult"
elif 65 <= age <= 125:
return "Senior"
else:
return "Not a valid age!"
# This is a lot of tests! Let's try them incrementally.
print(categorize_age(0))
print(categorize_age(1))
print(categorize_age(4))
print(categorize_age(12))
print(categorize_age(19))
print(categorize_age(54))
print(categorize_age(72))
print(categorize_age(99))
print(categorize_age(173))
print(categorize_age(-1))
```
%% Output
Baby
Baby
Toddler
Child
Adult
Adult
Senior
Senior
Not a valid age!
Not a valid age!
%% Cell type:markdown id: tags:
## Date Printer
See the requirements below. We will first break down the problem into smaller problems and right a function for each.
%% Cell type:markdown id: tags:
![date%20printer.png](attachment:date%20printer.png)
%% Cell type:code id: tags:
``` python
# first function: convert a month (int) into a 3 letter abbreviation or "N/A"
def month_to_str(month):
"""Convert a month (as an integer) into a string. 1 is Jan, 2 is Feb, etc."""
if month == 1:
return "Jan"
elif month == 2:
return "Feb"
elif month == 3:
return "Mar"
elif month == 4:
return "Apr"
elif month == 5:
return "May"
elif month == 6:
return "Jun"
elif month == 7:
return "Jul"
elif month == 8:
return "Aug"
elif month == 9:
return "Sep"
elif month == 10:
return "Oct"
elif month == 11:
return "Nov"
elif month == 12:
return "Dec"
else:
return "N/A"
print(month_to_str(1))
print(month_to_str(8))
print(month_to_str(12))
print(month_to_str(-1))
```
%% Output
Jan
Aug
Dec
N/A
%% Cell type:code id: tags:
``` python
# second function: convert a day (int) into a string writing '15th', '23rd' or "N/A"
def day_to_str(day):
"""Covert a day into a date string with proper ending.
16 --> '16th', 23 --> '23rd', """
# We want: To get the last digit of the day
if day < 0:
return 'N/A'
else:
end_digit = day % 10
ends_in_tens = ((day % 100) // 10) == 1
if end_digit == 1 and not ends_in_tens:
return str(day) + 'st'
elif end_digit == 2 and not ends_in_tens:
return str(day) + 'nd'
elif end_digit == 3 and not ends_in_tens:
return str(day) + 'rd'
else:
return str(day) + 'th'
print(day_to_str(1))
print(day_to_str(4))
print(day_to_str(6))
print(day_to_str(11))
print(day_to_str(14))
print(day_to_str(21))
print(day_to_str(52))
print(day_to_str(-1))
```
%% Output
1st
4th
6th
11th
14th
21st
52nd
N/A
%% Cell type:code id: tags:
``` python
# third function: convert a year (int) into a string for year
# 2021 ---> '21 # return a string with ' and the last 2 digits
# 1996 ---> 1996 # if year before 2000 return all 4 digits, as a string
def year_to_str(year):
"""Convert a year (as an integer) into a string. If the year is < 2000, return the full 4 digits
Otherwise, return the last 2 digits with a single quote in front"""
if(year < 2000):
return str(year)
else:
if (year % 100) // 10 == 0:
return '\'0' + str(year % 100)
else:
return '\'' + str(year % 100)
print(year_to_str(2022))
print(year_to_str(2004))
print(year_to_str(2010))
print(year_to_str(1997))
print(year_to_str(1462))
print(year_to_str(181))
```
%% Output
'22
'04
'10
1997
1462
181
%% Cell type:code id: tags:
``` python
# now put it all together
def format_date(year=2021, month=1, day=1):
return month_to_str(month) + ' ' + day_to_str(day) + ' of ' + year_to_str(year)
print(format_date())
print(format_date(2007))
print(format_date(day=23, year=2006, month=9))
print(format_date(2022, 2, 11))
print(format_date(1997, 10, 22))
print(format_date(1497, 6, 8))
print(format_date(-1, -1, -1))
```
%% Output
Jan 1st of '21
Jan 1st of '07
Sep 23rd of '06
Feb 11th of '22
Oct 22nd of 1997
Jun 8th of 1497
N/A N/A of -1
%% Cell type:code id: tags:
``` python
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment