Skip to content
Snippets Groups Projects
Commit 9036dcc6 authored by gsingh58's avatar gsingh58
Browse files

Lec9 update

parent ac259682
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:94154e6c tags:
# Announcements - Monday
* Download files for today's lecture
* [Exam 1 Conflict Form](https://docs.google.com/forms/d/e/1FAIpQLScKmey733tOjLfJ2q13Np76w_4o6cFcGVyNqWZmseZH0KNp2w/viewform)
* General: Wednesday February 22: 5:45 pm
* McBurney: Wednesday Februrary 22 5:30 pm - If you are approved for anything other than extra time and small group testing please fill out the form
* Alternate: Thursday February 23 5:45 pm
* Location will be sent by email on Monday February 20
* Note: Chemistry has an exam at the same time as our Wednesday exam
* Note: Wednesday Evening section
- alternate time 7:30 - 9:00 pm
- please fill out the conflict form to get on the list for the location email
- feel free to revise your conflict form if this time works better for you
* [The best way to study for the exam!!!](https://git.doit.wisc.edu/cdis/cs/courses/cs220/cs220-lecture-material/-/tree/main/s23/old_exams/exam1)
%% Cell type:markdown id:fcdba72c tags: %% Cell type:markdown id:fcdba72c tags:
# Conditionals 2 # Conditionals 2
## Readings ## Readings
- Parts of Chapter 5 of Think Python - Parts of Chapter 5 of Think Python
- Chapter 4.6 to end (skip 4.7) of Python for Everybody - Chapter 4.6 to end (skip 4.7) of Python for Everybody
%% Cell type:markdown id:5d944b30 tags: %% Cell type:markdown id:5d944b30 tags:
## Learning Objectives ## Learning Objectives
- Read and write nested conditional statements - Read and write nested conditional statements
- Define, state reasons for, and provide examples of refactoring - Define, state reasons for, and provide examples of refactoring
- Refactor code containing conditional statements into boolean expressions - Refactor code containing conditional statements into boolean expressions
%% Cell type:markdown id:cc7d8729 tags: %% Cell type:markdown id:cc7d8729 tags:
### Warmup ### Warmup
%% Cell type:markdown id:ab5912f2 tags: %% Cell type:markdown id:ab5912f2 tags:
What's wrong with this code? What's wrong with this code?
%% Cell type:code id:ececa83c tags: %% Cell type:code id:ececa83c tags:
``` python ``` python
team = "Badgers" team = "Badgers"
# if team == "Gophers" or "Hawkeyes" or "Spartans": # code with bug # if team == "Gophers" or "Hawkeyes" or "Spartans": # code with bug
if team == "Gophers" or team == "Hawkeyes" or team == "Spartans": if team == "Gophers" or team == "Hawkeyes" or team == "Spartans":
# fix: problem == has higher precedence than "or" # fix: problem == has higher precedence than "or"
# conditions should always be written with expansions # conditions should always be written with expansions
print("Ahh no badgers!") print("Ahh no badgers!")
else: else:
print("Yay, go badgers!") print("Yay, go badgers!")
``` ```
%% Output %% Output
Yay, go badgers! Yay, go badgers!
%% Cell type:markdown id:0f207c28 tags: %% Cell type:markdown id:0f207c28 tags:
**Find a partner to do these TODOs** **Find a partner to do these TODOs**
TODO: try to compute the boolean expression yourself, by inserting a new cell below. Change team variable's value to all three possible values. TODO: try to compute the boolean expression yourself, by inserting a new cell below. Change team variable's value to all three possible values.
TODO: fix the expression after your experiments TODO: fix the expression after your experiments
%% Cell type:code id:30bb2225 tags: %% Cell type:code id:30bb2225 tags:
``` python ``` python
team = "Badgers" team = "Badgers"
print(team == "Gophers" or "Hawkeyes" or "Spartans") print(team == "Gophers" or "Hawkeyes" or "Spartans")
``` ```
%% Output %% Output
Hawkeyes Hawkeyes
%% Cell type:code id:277df345 tags: %% Cell type:code id:277df345 tags:
``` python ``` python
team = "Gophers" team = "Gophers"
print(team == "Gophers" or "Hawkeyes" or "Spartans") print(team == "Gophers" or "Hawkeyes" or "Spartans")
``` ```
%% Output %% Output
True True
%% Cell type:markdown id:beeb3adf tags: %% Cell type:markdown id:beeb3adf tags:
## Example 1: Classifying Children by Age ## Example 1: Classifying Children by Age
- Chained conditionals: - Chained conditionals:
- multiple branch within the same conditional - multiple branch within the same conditional
- connected conditions - connected conditions
- if conditional branch - if conditional branch
- elif subjective conditional branch(es) - elif subjective conditional branch(es)
- elif conditions are subjective to the if condition - elif conditions are subjective to the if condition
- that is, only when if condition evaluates to `False`, the next elif condition is evaluated - that is, only when if condition evaluates to `False`, the next elif condition is evaluated
- only when the first elif condition evaluates to `False`, the second elif condition is evaluated - only when the first elif condition evaluates to `False`, the second elif condition is evaluated
- and so on ... - and so on ...
- else alternate branch - else alternate branch
Age classification: Age classification:
- "baby" 0 to 1 - "baby" 0 to 1
- "toddler" 2 to 3 - "toddler" 2 to 3
- "kid" 4 to 10 - "kid" 4 to 10
- "tween" 11 to 12 - "tween" 11 to 12
- "teen" 13 to 17 - "teen" 13 to 17
- "adult" 18+ (alternate execution) - "adult" 18+ (alternate execution)
%% Cell type:code id:1cec8460 tags: %% Cell type:code id:1cec8460 tags:
``` python ``` python
age = int(input("Enter your age: ")) age = int(input("Enter your age: "))
def categorize_age(age): def categorize_age(age):
if 0 <= age <= 1: if 0 <= age <= 1:
return "infant" return "infant"
elif 2 <= age <= 3: elif 2 <= age <= 3:
return "toddler" return "toddler"
elif 4 <= age <= 10: elif 4 <= age <= 10:
return "kid" return "kid"
elif 11 <= age <= 12: elif 11 <= age <= 12:
return "tween" return "tween"
elif 13 <= age <= 17: elif 13 <= age <= 17:
return "teen" return "teen"
else: else:
return "adult" return "adult"
print("You are a:", categorize_age(age)) print("You are a:", categorize_age(age))
``` ```
%% Output %% Output
Enter your age: 10 Enter your age: 10
You are a: kid You are a: kid
%% Cell type:markdown id:8fd63040 tags: %% Cell type:markdown id:8fd63040 tags:
## Example 2: Date Printer ## Example 2: Date Printer
- converts 2/14/2022 to "Feb 14th of ‘22" - converts 2/14/2022 to "Feb 14th of ‘22"
- decompose a big problem into appropriate steps, by writing functions for individual components - decompose a big problem into appropriate steps, by writing functions for individual components
%% Cell type:code id:7c72488a tags: %% Cell type:code id:7c72488a tags:
``` python ``` python
def format_month(month): def format_month(month):
"""Convert a month (as an integer) into a string. """Convert a month (as an integer) into a string.
1 is Jan, 2 is Feb, etc.""" 1 is Jan, 2 is Feb, etc."""
if month == 1: if month == 1:
return "Jan" return "Jan"
elif month == 2: elif month == 2:
return "Feb" return "Feb"
elif month == 3: elif month == 3:
return "Mar" return "Mar"
elif month == 4: elif month == 4:
return "Apr" return "Apr"
elif month == 5: elif month == 5:
return "May" return "May"
elif month == 6: elif month == 6:
return "Jun" return "Jun"
elif month == 7: elif month == 7:
return "Jul" return "Jul"
elif month == 8: elif month == 8:
return "Aug" return "Aug"
elif month == 9: elif month == 9:
return "Sep" return "Sep"
elif month == 10: elif month == 10:
return "Oct" return "Oct"
elif month == 11: elif month == 11:
return "Nov" return "Nov"
elif month == 12: elif month == 12:
return "Dec" return "Dec"
else: else:
return "Invalid month!" return "Invalid month!"
``` ```
%% Cell type:code id:71bf7f2d tags: %% Cell type:code id:71bf7f2d tags:
``` python ``` python
def format_day(day): def format_day(day):
"""Covert a day into a date string with proper ending. """Covert a day into a date string with proper ending.
16 --> '16th', 23 --> '23rd', """ 16 --> '16th', 23 --> '23rd', """
suffix = "" suffix = ""
if day < 1: if day < 1:
suffix = "error" suffix = "error"
elif 11 <= day % 100 <= 20: elif 11 <= day % 100 <= 20:
suffix = str(day) + "th" suffix = str(day) + "th"
elif day % 10 == 1: elif day % 10 == 1:
suffix = str(day) + "st" suffix = str(day) + "st"
elif day % 10 == 2: elif day % 10 == 2:
suffix = str(day) + "nd" suffix = str(day) + "nd"
elif day % 10 == 3: elif day % 10 == 3:
suffix = str(day) + "rd" suffix = str(day) + "rd"
else: else:
suffix = str(day) + "th" suffix = str(day) + "th"
return suffix return suffix
print(format_day(1)) print(format_day(1))
print(format_day(2)) print(format_day(2))
print(format_day(3)) print(format_day(3))
print(format_day(11)) print(format_day(11))
print(format_day(21)) print(format_day(21))
print(format_day(111)) print(format_day(111))
print(format_day(-1)) print(format_day(-1))
``` ```
%% Output %% Output
1st 1st
2nd 2nd
3rd 3rd
11th 11th
21st 21st
111th 111th
error error
%% Cell type:code id:c142acc7 tags: %% Cell type:code id:c142acc7 tags:
``` python ``` python
def format_date(month, day, year): def format_date(month, day, year):
"""returns a string representing the date, such as Feb 11th of ‘22""" """returns a string representing the date, such as Feb 11th of ‘22"""
result_str = "" result_str = ""
result_str += format_month(month) result_str += format_month(month)
result_str += " " + format_day(day) result_str += " " + format_day(day)
result_str += " of '" + str(year % 100) result_str += " of '" + str(year % 100)
return result_str return result_str
``` ```
%% Cell type:code id:e45d4fd3 tags: %% Cell type:code id:e45d4fd3 tags:
``` python ``` python
format_date(9, 26, 2022) format_date(9, 26, 2022)
``` ```
%% Output %% Output
"Sep 26th of '22" "Sep 26th of '22"
%% Cell type:markdown id:efc4659c tags: %% Cell type:markdown id:efc4659c tags:
## Example 3: Stoplight ## Example 3: Stoplight
<div> <div>
<img src="attachment:Stoplight.png" width="600"/> <img src="attachment:Stoplight.png" width="600"/>
</div> </div>
%% Cell type:code id:fc1cec93 tags: %% Cell type:code id:fc1cec93 tags:
``` python ``` python
def stop_light(color, distance): def stop_light(color, distance):
if color == 'red': if color == 'red':
if distance < 15: if distance < 15:
return "hit the gas" return "hit the gas"
else: else:
return "stop abruptly" return "stop abruptly"
elif color == 'yellow': elif color == 'yellow':
if distance < 30: if distance < 30:
return "continue at same speed" return "continue at same speed"
else: else:
return "stop" return "stop"
elif color == "green": elif color == "green":
return "smile :)" return "smile :)"
else: else:
return "Invalid color!" return "Invalid color!"
``` ```
%% Cell type:markdown id:6fd52715 tags: %% Cell type:markdown id:6fd52715 tags:
## Refactoring ## Refactoring
What is it? What is it?
- Improving/rewriting parts of a program without changing its behavior. - Improving/rewriting parts of a program without changing its behavior.
- Like a re-wording of a recipe without changing it - Like a re-wording of a recipe without changing it
Why do it? Why do it?
- Make it easier to read and understand the code & what it's doing - Make it easier to read and understand the code & what it's doing
- Sometimes to make code run faster - Sometimes to make code run faster
Principles of good refactoring: Principles of good refactoring:
- Don't change the program's behavior! - Don't change the program's behavior!
- The program should end up more readable than it started - The program should end up more readable than it started
- Use knowledge of if/else, and/or/not, ==, !=, etc. - Use knowledge of if/else, and/or/not, ==, !=, etc.
%% Cell type:markdown id:b5ad668e tags: %% Cell type:markdown id:b5ad668e tags:
### Refactoring example 1: nested if conditions ### Refactoring example 1: nested if conditions
%% Cell type:code id:d391cc8a tags: %% Cell type:code id:d391cc8a tags:
``` python ``` python
# check combination of a lock # check combination of a lock
def check_combination(a, b, c): def check_combination(a, b, c):
if a == 2: if a == 2:
if b == 2: if b == 2:
if c == 0: if c == 0:
return True return True
else: else:
return False return False
else: else:
return False return False
else: else:
return False return False
print(check_combination(2, 2, 0)) print(check_combination(2, 2, 0))
print(check_combination(2, 1, 0)) print(check_combination(2, 1, 0))
print(check_combination(1, 2, 0)) print(check_combination(1, 2, 0))
print(check_combination(3, 1, 9)) print(check_combination(3, 1, 9))
``` ```
%% Output %% Output
True True
False False
False False
False False
%% Cell type:code id:e5caed75 tags: %% Cell type:code id:e5caed75 tags:
``` python ``` python
def bad_combo(a, b, c): def bad_combo(a, b, c):
return a == 2 or b == 2 or c == 0 return a == 2 or b == 2 or c == 0
print(bad_combo(2, 2, 0)) print(bad_combo(2, 2, 0))
print(bad_combo(2, 1, 0)) print(bad_combo(2, 1, 0))
print(bad_combo(1, 2, 0)) print(bad_combo(1, 2, 0))
print(bad_combo(3, 1, 9)) print(bad_combo(3, 1, 9))
``` ```
%% Output %% Output
True True
True True
True True
False False
%% Cell type:code id:b69cadc1 tags: %% Cell type:code id:b69cadc1 tags:
``` python ``` python
def refactor_combo(a, b, c): def refactor_combo(a, b, c):
return a == 2 and b == 2 and c == 0 return a == 2 and b == 2 and c == 0
print(refactor_combo(2, 2, 0)) print(refactor_combo(2, 2, 0))
print(refactor_combo(2, 1, 0)) print(refactor_combo(2, 1, 0))
print(refactor_combo(1, 2, 0)) print(refactor_combo(1, 2, 0))
print(refactor_combo(3, 1, 9)) print(refactor_combo(3, 1, 9))
``` ```
%% Output %% Output
True True
False False
False False
False False
%% Cell type:markdown id:ef5852ea tags: %% Cell type:markdown id:ef5852ea tags:
### Refactoring example 2: cluttered conditional ### Refactoring example 2: cluttered conditional
%% Cell type:code id:9e45e8f8 tags: %% Cell type:code id:9e45e8f8 tags:
``` python ``` python
def check_different(b1, b2): def check_different(b1, b2):
if b1 == True and b2 == False: if b1 == True and b2 == False:
return True return True
elif b1 == False and b2 == True: elif b1 == False and b2 == True:
return True return True
elif b1 == True and b2 == True: elif b1 == True and b2 == True:
return False return False
elif b1 == False and b2 == False: elif b1 == False and b2 == False:
return False return False
print(check_different(True, False)) print(check_different(True, False))
print(check_different(False, False)) print(check_different(False, False))
print(check_different(False, True)) print(check_different(False, True))
print(check_different(True, True)) print(check_different(True, True))
``` ```
%% Output %% Output
True True
False False
True True
False False
%% Cell type:code id:7b625604 tags: %% Cell type:code id:7b625604 tags:
``` python ``` python
def refactor_check_different(b1, b2): def refactor_check_different(b1, b2):
return b1 != b2 return b1 != b2
print(refactor_check_different(True, False)) print(refactor_check_different(True, False))
print(refactor_check_different(False, False)) print(refactor_check_different(False, False))
print(refactor_check_different(False, True)) print(refactor_check_different(False, True))
print(refactor_check_different(True, True)) print(refactor_check_different(True, True))
``` ```
%% Output %% Output
True True
False False
True True
False False
%% Cell type:markdown id:a488bc9c tags: %% Cell type:markdown id:a488bc9c tags:
## After lecture ## After lecture
- go through examples in slides - go through examples in slides
- reason about every refactor version and predict correctness of the refactor - reason about every refactor version and predict correctness of the refactor
- try out each refactor example using Interactive Exercises - try out each refactor example using Interactive Exercises
- try these sample exam questions on refactoring (predict output, then type your code and run, to confirm your output) - try these sample exam questions on refactoring (predict output, then type your code and run, to confirm your output)
%% Cell type:code id:2ba3bbcc tags: %% Cell type:code id:2ba3bbcc tags:
``` python ``` python
# Refactoring Practice 1: recognizing equivalent code # Refactoring Practice 1: recognizing equivalent code
# Exam 1 Fall 2020 # Exam 1 Fall 2020
def g(x, y): def g(x, y):
# the expression after an if must be a Boolean expression or have a Boolean value # the expression after an if must be a Boolean expression or have a Boolean value
if x: if x:
if y: if y:
return True return True
else: else:
return False return False
else: else:
return False return False
# TODO: try all combinations of intitializations to b1 and b2 # TODO: try all combinations of intitializations to b1 and b2
b1 = True b1 = True
b2 = True b2 = True
g(b1, b2) g(b1, b2)
# Which of the following will give the same result in all cases, as g(b1, b2)? # Which of the following will give the same result in all cases, as g(b1, b2)?
# a.) b1 != b2 # a.) b1 != b2
# b.) b1 and b2 # b.) b1 and b2
# c.) b1 == b2 # c.) b1 == b2
# d.) b1 or b2 # d.) b1 or b2
``` ```
%% Output %% Output
True True
%% Cell type:code id:d4a4d099 tags: %% Cell type:code id:d4a4d099 tags:
``` python ``` python
# Refactoring Practice 2: # Refactoring Practice 2:
def h(x, y): def h(x, y):
# the expression after an if must be a Boolean expression or have a Boolean value # the expression after an if must be a Boolean expression or have a Boolean value
if x: if x:
return False return False
else: else:
if y: if y:
return False return False
else: else:
return True return True
# TODO: try all combinations of intitializations to b1 and b2 # TODO: try all combinations of intitializations to b1 and b2
b1 = True b1 = True
b2 = True b2 = True
h(b1, b2) h(b1, b2)
# Which of the following will give the same result in all cases, as h(b1, b2) ? # Which of the following will give the same result in all cases, as h(b1, b2) ?
# a.) b1 != b2 # a.) b1 != b2
# b.) b1 and b2 # b.) b1 and b2
# c.) b1 == b2 # c.) b1 == b2
# d.) not b1 and not b2 # d.) not b1 and not b2
``` ```
%% Output %% Output
False False
%% Cell type:code id:0d2cfee6 tags: %% Cell type:code id:0d2cfee6 tags:
``` python ``` python
# Refactoring Practice 3: # Refactoring Practice 3:
def some_bool_eval(x, y): def some_bool_eval(x, y):
# the expression after an if must be a Boolean expression or have a Boolean value # the expression after an if must be a Boolean expression or have a Boolean value
if x: if x:
return True return True
elif y: elif y:
return True return True
else: else:
return False return False
print(some_bool_eval(True, False)) print(some_bool_eval(True, False))
print(some_bool_eval(False, True)) print(some_bool_eval(False, True))
print(some_bool_eval(True, True)) print(some_bool_eval(True, True))
print(some_bool_eval(False, False)) print(some_bool_eval(False, False))
# what is the best way to refactor the body of the function ? # what is the best way to refactor the body of the function ?
# A. return x and y # A. return x and y
# B. return x or y # B. return x or y
# C. return x != y # C. return x != y
# D. return x == y # D. return x == y
``` ```
%% Output %% Output
True True
True True
True True
False False
......
%% Cell type:markdown id:53aab2e8 tags:
# Announcements - Monday
* Download files for today's lecture
* [Exam 1 Conflict Form](https://docs.google.com/forms/d/e/1FAIpQLScKmey733tOjLfJ2q13Np76w_4o6cFcGVyNqWZmseZH0KNp2w/viewform)
* General: Wednesday February 22: 5:45 pm
* McBurney: Wednesday Februrary 22 5:30 pm - If you are approved for anything other than extra time and small group testing please fill out the form
* Alternate: Thursday February 23 5:45 pm
* Location will be sent by email on Monday February 20
* Note: Chemistry has an exam at the same time as our Wednesday exam
* Note: Wednesday Evening section
- alternate time 7:30 - 9:00 pm
- please fill out the conflict form to get on the list for the location email
- feel free to revise your conflict form if this time works better for you
* [The best way to study for the exam!!!](https://git.doit.wisc.edu/cdis/cs/courses/cs220/cs220-lecture-material/-/tree/main/s23/old_exams/exam1)
%% Cell type:markdown id:25751b55 tags: %% Cell type:markdown id:25751b55 tags:
# Conditionals 2 # Conditionals 2
## Readings ## Readings
- Parts of Chapter 5 of Think Python - Parts of Chapter 5 of Think Python
- Chapter 4.6 to end (skip 4.7) of Python for Everybody - Chapter 4.6 to end (skip 4.7) of Python for Everybody
%% Cell type:markdown id:d29fedb7 tags: %% Cell type:markdown id:d29fedb7 tags:
## Learning Objectives ## Learning Objectives
- Read and write nested conditional statements - Read and write nested conditional statements
- Define, state reasons for, and provide examples of refactoring - Define, state reasons for, and provide examples of refactoring
- Refactor code containing conditional statements into boolean expressions - Refactor code containing conditional statements into boolean expressions
%% Cell type:markdown id:00a02ca4 tags: %% Cell type:markdown id:00a02ca4 tags:
### Warmup ### Warmup
%% Cell type:markdown id:4058219f tags: %% Cell type:markdown id:4058219f tags:
What's wrong with this code? What's wrong with this code?
%% Cell type:code id:909ef1b6 tags: %% Cell type:code id:909ef1b6 tags:
``` python ``` python
team = "Badgers" team = "Badgers"
if team == "Gophers" or "Hawkeyes" or "Spartans": if team == "Gophers" or "Hawkeyes" or "Spartans":
print("Ahh no badgers!") print("Ahh no badgers!")
else: else:
print("Yay, go badgers!") print("Yay, go badgers!")
``` ```
%% Cell type:markdown id:a9579e28 tags: %% Cell type:markdown id:a9579e28 tags:
**Find a partner to do these TODOs** **Find a partner to do these TODOs**
TODO: try to compute the boolean expression yourself, by inserting a new cell below. Change team variable's value to all three possible values. Observe what is wrong with the current expression. TODO: try to compute the boolean expression yourself, by inserting a new cell below. Change team variable's value to all three possible values. Observe what is wrong with the current expression.
TODO: fix the expression after your experiments TODO: fix the expression after your experiments
%% Cell type:markdown id:dc60b188 tags: %% Cell type:markdown id:dc60b188 tags:
## Example 1: Classifying Children by Age ## Example 1: Classifying Children by Age
- Chained conditionals: - Chained conditionals:
- multiple branch within the same conditional - multiple branch within the same conditional
- connected conditions - connected conditions
- if conditional branch - if conditional branch
- elif subjective conditional branch(es) - elif subjective conditional branch(es)
- elif conditions are subjective to the if condition - elif conditions are subjective to the if condition
- that is, only when if condition evaluates to `False`, the next elif condition is evaluated - that is, only when if condition evaluates to `False`, the next elif condition is evaluated
- only when the first elif condition evaluates to `False`, the second elif condition is evaluated - only when the first elif condition evaluates to `False`, the second elif condition is evaluated
- and so on ... - and so on ...
- else alternate branch - else alternate branch
Age classification: Age classification:
- "baby" 0 to 1 - "baby" 0 to 1
- "toddler" 2 to 3 - "toddler" 2 to 3
- "kid" 4 to 10 - "kid" 4 to 10
- "tween" 11 to 12 - "tween" 11 to 12
- "teen" 13 to 17 - "teen" 13 to 17
- "adult" 18+ (alternate execution) - "adult" 18+ (alternate execution)
%% Cell type:code id:2ff08fe4 tags: %% Cell type:code id:2ff08fe4 tags:
``` python ``` python
# TODO: get age as user input # TODO: get age as user input
age = int(input("Enter your age: ")) age = int(input("Enter your age: "))
def categorize_age(age): def categorize_age(age):
if 0 <= age <= 1: if 0 <= age <= 1:
return "infant" # TODO: discuss why we have a return here instead of print return "infant" # TODO: discuss why we have a return here instead of print
elif 2 <= age <= 3: elif 2 <= age <= 3:
return "toddler" return "toddler"
elif 4 <= age <= 10: elif 4 <= age <= 10:
return "kid" return "kid"
elif 11 <= age <= 12: elif 11 <= age <= 12:
return "tween" return "tween"
elif 13 <= age <= 17: elif 13 <= age <= 17:
return "teen" return "teen"
else: else:
return "adult" return "adult"
print("You are a:", categorize_age(age)) print("You are a:", categorize_age(age))
``` ```
%% Cell type:markdown id:def170db tags: %% Cell type:markdown id:def170db tags:
## Example 2: Date Printer ## Example 2: Date Printer
- converts 2/14/2022 to "Feb 14th of ‘22" - converts 2/14/2022 to "Feb 14th of ‘22"
- decompose a big problem into appropriate steps, by writing functions for individual components - decompose a big problem into appropriate steps, by writing functions for individual components
%% Cell type:code id:8c627afc tags: %% Cell type:code id:8c627afc tags:
``` python ``` python
def format_month(month): def format_month(month):
"""Convert a month (as an integer) into a string. """Convert a month (as an integer) into a string.
1 is Jan, 2 is Feb, etc.""" 1 is Jan, 2 is Feb, etc."""
pass pass
``` ```
%% Cell type:code id:4207cb50 tags: %% Cell type:code id:4207cb50 tags:
``` python ``` python
def format_day(day): def format_day(day):
"""Covert a day into a date string with proper ending. """Covert a day into a date string with proper ending.
16 --> '16th', 23 --> '23rd', """ 16 --> '16th', 23 --> '23rd', """
suffix = "" suffix = ""
# TODO: write the conditional here # TODO: write the conditional here
return suffix return suffix
print(format_day(1)) print(format_day(1))
print(format_day(2)) print(format_day(2))
print(format_day(3)) print(format_day(3))
print(format_day(11)) print(format_day(11))
print(format_day(21)) print(format_day(21))
print(format_day(111)) print(format_day(111))
print(format_day(-1)) print(format_day(-1))
``` ```
%% Cell type:code id:918b726c tags: %% Cell type:code id:918b726c tags:
``` python ``` python
def format_date(month, day, year): def format_date(month, day, year):
"""returns a string representing the date, such as Feb 11th of ‘22""" """returns a string representing the date, such as Feb 11th of ‘22"""
pass pass
``` ```
%% Cell type:code id:92cde74e tags: %% Cell type:code id:92cde74e tags:
``` python ``` python
format_date(2, 14, 2022) format_date(2, 14, 2022)
``` ```
%% Cell type:markdown id:528d781f tags: %% Cell type:markdown id:528d781f tags:
## Example 3: Stoplight ## Example 3: Stoplight
<div> <div>
<img src="attachment:Stoplight.png" width="600"/> <img src="attachment:Stoplight.png" width="600"/>
</div> </div>
%% Cell type:code id:3c0c3bcd tags: %% Cell type:code id:3c0c3bcd tags:
``` python ``` python
def stop_light(color, distance): def stop_light(color, distance):
pass pass
``` ```
%% Cell type:markdown id:459296f2 tags: %% Cell type:markdown id:459296f2 tags:
## Refactoring ## Refactoring
What is it? What is it?
- Improving/rewriting parts of a program without changing its behavior. - Improving/rewriting parts of a program without changing its behavior.
- Like a re-wording of a recipe without changing it - Like a re-wording of a recipe without changing it
Why do it? Why do it?
- Make it easier to read and understand the code & what it's doing - Make it easier to read and understand the code & what it's doing
- Sometimes to make code run faster - Sometimes to make code run faster
Principles of good refactoring: Principles of good refactoring:
- Don't change the program's behavior! - Don't change the program's behavior!
- The program should end up more readable than it started - The program should end up more readable than it started
- Use knowledge of if/else, and/or/not, ==, !=, etc. - Use knowledge of if/else, and/or/not, ==, !=, etc.
%% Cell type:markdown id:d5aae7eb tags: %% Cell type:markdown id:d5aae7eb tags:
### Refactoring example 1: nested if conditions ### Refactoring example 1: nested if conditions
%% Cell type:code id:d03bc22e tags: %% Cell type:code id:d03bc22e tags:
``` python ``` python
# check combination of a lock # check combination of a lock
def check_combination(a, b, c): def check_combination(a, b, c):
if a == 2: if a == 2:
if b == 2: if b == 2:
if c == 0: if c == 0:
return True return True
else: else:
return False return False
else: else:
return False return False
else: else:
return False return False
print(check_combination(2, 2, 0)) print(check_combination(2, 2, 0))
print(check_combination(2, 1, 0)) print(check_combination(2, 1, 0))
print(check_combination(1, 2, 0)) print(check_combination(1, 2, 0))
print(check_combination(3, 1, 9)) print(check_combination(3, 1, 9))
``` ```
%% Cell type:code id:feddb4b9 tags: %% Cell type:code id:feddb4b9 tags:
``` python ``` python
def bad_combo(a, b, c): def bad_combo(a, b, c):
pass pass
print(bad_combo(2, 2, 0)) print(bad_combo(2, 2, 0))
print(bad_combo(2, 1, 0)) print(bad_combo(2, 1, 0))
print(bad_combo(1, 2, 0)) print(bad_combo(1, 2, 0))
print(bad_combo(3, 1, 9)) print(bad_combo(3, 1, 9))
``` ```
%% Cell type:code id:b30eed5f tags: %% Cell type:code id:b30eed5f tags:
``` python ``` python
def refactor_combo(a, b, c): def refactor_combo(a, b, c):
pass pass
print(refactor_combo(2, 2, 0)) print(refactor_combo(2, 2, 0))
print(refactor_combo(2, 1, 0)) print(refactor_combo(2, 1, 0))
print(refactor_combo(1, 2, 0)) print(refactor_combo(1, 2, 0))
print(refactor_combo(3, 1, 9)) print(refactor_combo(3, 1, 9))
``` ```
%% Cell type:markdown id:8b7f186f tags: %% Cell type:markdown id:8b7f186f tags:
### Refactoring example 2: cluttered conditional ### Refactoring example 2: cluttered conditional
%% Cell type:code id:5285840d tags: %% Cell type:code id:5285840d tags:
``` python ``` python
def check_different(b1, b2): def check_different(b1, b2):
if b1 == True and b2 == False: if b1 == True and b2 == False:
return True return True
elif b1 == False and b2 == True: elif b1 == False and b2 == True:
return True return True
elif b1 == True and b2 == True: elif b1 == True and b2 == True:
return False return False
elif b1 == False and b2 == False: elif b1 == False and b2 == False:
return False return False
print(check_different(True, False)) print(check_different(True, False))
print(check_different(False, False)) print(check_different(False, False))
print(check_different(False, True)) print(check_different(False, True))
print(check_different(True, True)) print(check_different(True, True))
``` ```
%% Cell type:code id:e53c2cfe tags: %% Cell type:code id:e53c2cfe tags:
``` python ``` python
def refactor_check_different(b1, b2): def refactor_check_different(b1, b2):
pass pass
print(refactor_check_different(True, False)) print(refactor_check_different(True, False))
print(refactor_check_different(False, False)) print(refactor_check_different(False, False))
print(refactor_check_different(False, True)) print(refactor_check_different(False, True))
print(refactor_check_different(True, True)) print(refactor_check_different(True, True))
``` ```
%% Cell type:markdown id:5dba6585 tags: %% Cell type:markdown id:5dba6585 tags:
## After lecture ## After lecture
- go through examples in slides - go through examples in slides
- reason about every refactor version and predict correctness of the refactor - reason about every refactor version and predict correctness of the refactor
- try out each refactor example using Interactive Exercises - try out each refactor example using Interactive Exercises
- try these sample exam questions on refactoring (predict output, then type your code and run, to confirm your output) - try these sample exam questions on refactoring (predict output, then type your code and run, to confirm your output)
%% Cell type:code id:80c45d13 tags: %% Cell type:code id:80c45d13 tags:
``` python ``` python
# Refactoring Practice 1: recognizing equivalent code # Refactoring Practice 1: recognizing equivalent code
# Exam 1 Fall 2020 # Exam 1 Fall 2020
def g(x, y): def g(x, y):
# the expression after an if must be a Boolean expression or have a Boolean value # the expression after an if must be a Boolean expression or have a Boolean value
if x: if x:
if y: if y:
return True return True
else: else:
return False return False
else: else:
return False return False
# Which of the following will give the same result in all cases, as g(b1, b2)? # Which of the following will give the same result in all cases, as g(b1, b2)?
# a.) b1 != b2 # a.) b1 != b2
# b.) b1 and b2 # b.) b1 and b2
# c.) b1 == b2 # c.) b1 == b2
# d.) b1 or b2 # d.) b1 or b2
``` ```
%% Cell type:code id:795e83e4 tags: %% Cell type:code id:795e83e4 tags:
``` python ``` python
# Refactoring Practice 2: # Refactoring Practice 2:
def h(x, y): def h(x, y):
# the expression after an if must be a Boolean expression or have a Boolean value # the expression after an if must be a Boolean expression or have a Boolean value
if x: if x:
return False return False
else: else:
if y: if y:
return False return False
else: else:
return True return True
# Which of the following will give the same result in all cases, as h(b1, b2) ? # Which of the following will give the same result in all cases, as h(b1, b2) ?
# a.) b1 != b2 # a.) b1 != b2
# b.) b1 and b2 # b.) b1 and b2
# c.) b1 == b2 # c.) b1 == b2
# d.) not b1 and not b2 # d.) not b1 and not b2
``` ```
%% Cell type:code id:6fca19ec tags: %% Cell type:code id:6fca19ec tags:
``` python ``` python
# Refactoring Practice 3: # Refactoring Practice 3:
def some_bool_eval(x, y): def some_bool_eval(x, y):
# the expression after an if must be a Boolean expression or have a Boolean value # the expression after an if must be a Boolean expression or have a Boolean value
if x: if x:
return True return True
elif y: elif y:
return True return True
else: else:
return False return False
print(some_bool_eval(True, False)) print(some_bool_eval(True, False))
print(some_bool_eval(False, True)) print(some_bool_eval(False, True))
print(some_bool_eval(True, True)) print(some_bool_eval(True, True))
print(some_bool_eval(False, False)) print(some_bool_eval(False, False))
# what is the best way to refactor the body of the function ? # what is the best way to refactor the body of the function ?
# A. return x and y # A. return x and y
# B. return x or y # B. return x or y
# C. return x != y # C. return x != y
# D. return x == y # D. return x == y
``` ```
......
%% Cell type:markdown id:4a88ba18 tags:
# Announcements - Monday
* Download files for today's lecture
* [Exam 1 Conflict Form](https://docs.google.com/forms/d/e/1FAIpQLScKmey733tOjLfJ2q13Np76w_4o6cFcGVyNqWZmseZH0KNp2w/viewform)
* General: Wednesday February 22: 5:45 pm
* McBurney: Wednesday Februrary 22 5:30 pm - If you are approved for anything other than extra time and small group testing please fill out the form
* Alternate: Thursday February 23 5:45 pm
* Location will be sent by email on Monday February 20
* Note: Chemistry has an exam at the same time as our Wednesday exam
* Note: Wednesday Evening section
- alternate time 7:30 - 9:00 pm
- please fill out the conflict form to get on the list for the location email
- feel free to revise your conflict form if this time works better for you
* [The best way to study for the exam!!!](https://git.doit.wisc.edu/cdis/cs/courses/cs220/cs220-lecture-material/-/tree/main/s23/old_exams/exam1)
%% Cell type:markdown id:25751b55 tags: %% Cell type:markdown id:25751b55 tags:
# Conditionals 2 # Conditionals 2
## Readings ## Readings
- Parts of Chapter 5 of Think Python - Parts of Chapter 5 of Think Python
- Chapter 4.6 to end (skip 4.7) of Python for Everybody - Chapter 4.6 to end (skip 4.7) of Python for Everybody
%% Cell type:markdown id:d29fedb7 tags: %% Cell type:markdown id:d29fedb7 tags:
## Learning Objectives ## Learning Objectives
- Read and write nested conditional statements - Read and write nested conditional statements
- Define, state reasons for, and provide examples of refactoring - Define, state reasons for, and provide examples of refactoring
- Refactor code containing conditional statements into boolean expressions - Refactor code containing conditional statements into boolean expressions
%% Cell type:markdown id:00a02ca4 tags: %% Cell type:markdown id:00a02ca4 tags:
### Warmup ### Warmup
%% Cell type:markdown id:4058219f tags: %% Cell type:markdown id:4058219f tags:
What's wrong with this code? What's wrong with this code?
%% Cell type:code id:909ef1b6 tags: %% Cell type:code id:909ef1b6 tags:
``` python ``` python
team = "Badgers" team = "Badgers"
if team == "Gophers" or "Hawkeyes" or "Spartans": if team == "Gophers" or "Hawkeyes" or "Spartans":
print("Ahh no badgers!") print("Ahh no badgers!")
else: else:
print("Yay, go badgers!") print("Yay, go badgers!")
``` ```
%% Cell type:markdown id:a9579e28 tags: %% Cell type:markdown id:a9579e28 tags:
**Find a partner to do these TODOs** **Find a partner to do these TODOs**
TODO: try to compute the boolean expression yourself, by inserting a new cell below. Change team variable's value to all three possible values. Observe what is wrong with the current expression. TODO: try to compute the boolean expression yourself, by inserting a new cell below. Change team variable's value to all three possible values. Observe what is wrong with the current expression.
TODO: fix the expression after your experiments TODO: fix the expression after your experiments
%% Cell type:markdown id:dc60b188 tags: %% Cell type:markdown id:dc60b188 tags:
## Example 1: Classifying Children by Age ## Example 1: Classifying Children by Age
- Chained conditionals: - Chained conditionals:
- multiple branch within the same conditional - multiple branch within the same conditional
- connected conditions - connected conditions
- if conditional branch - if conditional branch
- elif subjective conditional branch(es) - elif subjective conditional branch(es)
- elif conditions are subjective to the if condition - elif conditions are subjective to the if condition
- that is, only when if condition evaluates to `False`, the next elif condition is evaluated - that is, only when if condition evaluates to `False`, the next elif condition is evaluated
- only when the first elif condition evaluates to `False`, the second elif condition is evaluated - only when the first elif condition evaluates to `False`, the second elif condition is evaluated
- and so on ... - and so on ...
- else alternate branch - else alternate branch
Age classification: Age classification:
- "baby" 0 to 1 - "baby" 0 to 1
- "toddler" 2 to 3 - "toddler" 2 to 3
- "kid" 4 to 10 - "kid" 4 to 10
- "tween" 11 to 12 - "tween" 11 to 12
- "teen" 13 to 17 - "teen" 13 to 17
- "adult" 18+ (alternate execution) - "adult" 18+ (alternate execution)
%% Cell type:code id:2ff08fe4 tags: %% Cell type:code id:2ff08fe4 tags:
``` python ``` python
# TODO: get age as user input # TODO: get age as user input
age = int(input("Enter your age: ")) age = int(input("Enter your age: "))
def categorize_age(age): def categorize_age(age):
if 0 <= age <= 1: if 0 <= age <= 1:
return "infant" # TODO: discuss why we have a return here instead of print return "infant" # TODO: discuss why we have a return here instead of print
elif 2 <= age <= 3: elif 2 <= age <= 3:
return "toddler" return "toddler"
elif 4 <= age <= 10: elif 4 <= age <= 10:
return "kid" return "kid"
elif 11 <= age <= 12: elif 11 <= age <= 12:
return "tween" return "tween"
elif 13 <= age <= 17: elif 13 <= age <= 17:
return "teen" return "teen"
else: else:
return "adult" return "adult"
print("You are a:", categorize_age(age)) print("You are a:", categorize_age(age))
``` ```
%% Cell type:markdown id:def170db tags: %% Cell type:markdown id:def170db tags:
## Example 2: Date Printer ## Example 2: Date Printer
- converts 2/14/2022 to "Feb 14th of ‘22" - converts 2/14/2022 to "Feb 14th of ‘22"
- decompose a big problem into appropriate steps, by writing functions for individual components - decompose a big problem into appropriate steps, by writing functions for individual components
%% Cell type:code id:8c627afc tags: %% Cell type:code id:8c627afc tags:
``` python ``` python
def format_month(month): def format_month(month):
"""Convert a month (as an integer) into a string. """Convert a month (as an integer) into a string.
1 is Jan, 2 is Feb, etc.""" 1 is Jan, 2 is Feb, etc."""
pass pass
``` ```
%% Cell type:code id:4207cb50 tags: %% Cell type:code id:4207cb50 tags:
``` python ``` python
def format_day(day): def format_day(day):
"""Covert a day into a date string with proper ending. """Covert a day into a date string with proper ending.
16 --> '16th', 23 --> '23rd', """ 16 --> '16th', 23 --> '23rd', """
suffix = "" suffix = ""
# TODO: write the conditional here # TODO: write the conditional here
return suffix return suffix
print(format_day(1)) print(format_day(1))
print(format_day(2)) print(format_day(2))
print(format_day(3)) print(format_day(3))
print(format_day(11)) print(format_day(11))
print(format_day(21)) print(format_day(21))
print(format_day(111)) print(format_day(111))
print(format_day(-1)) print(format_day(-1))
``` ```
%% Cell type:code id:918b726c tags: %% Cell type:code id:918b726c tags:
``` python ``` python
def format_date(month, day, year): def format_date(month, day, year):
"""returns a string representing the date, such as Feb 11th of ‘22""" """returns a string representing the date, such as Feb 11th of ‘22"""
pass pass
``` ```
%% Cell type:code id:92cde74e tags: %% Cell type:code id:92cde74e tags:
``` python ``` python
format_date(2, 14, 2022) format_date(2, 14, 2022)
``` ```
%% Cell type:markdown id:528d781f tags: %% Cell type:markdown id:528d781f tags:
## Example 3: Stoplight ## Example 3: Stoplight
<div> <div>
<img src="attachment:Stoplight.png" width="600"/> <img src="attachment:Stoplight.png" width="600"/>
</div> </div>
%% Cell type:code id:3c0c3bcd tags: %% Cell type:code id:3c0c3bcd tags:
``` python ``` python
def stop_light(color, distance): def stop_light(color, distance):
pass pass
``` ```
%% Cell type:markdown id:459296f2 tags: %% Cell type:markdown id:459296f2 tags:
## Refactoring ## Refactoring
What is it? What is it?
- Improving/rewriting parts of a program without changing its behavior. - Improving/rewriting parts of a program without changing its behavior.
- Like a re-wording of a recipe without changing it - Like a re-wording of a recipe without changing it
Why do it? Why do it?
- Make it easier to read and understand the code & what it's doing - Make it easier to read and understand the code & what it's doing
- Sometimes to make code run faster - Sometimes to make code run faster
Principles of good refactoring: Principles of good refactoring:
- Don't change the program's behavior! - Don't change the program's behavior!
- The program should end up more readable than it started - The program should end up more readable than it started
- Use knowledge of if/else, and/or/not, ==, !=, etc. - Use knowledge of if/else, and/or/not, ==, !=, etc.
%% Cell type:markdown id:d5aae7eb tags: %% Cell type:markdown id:d5aae7eb tags:
### Refactoring example 1: nested if conditions ### Refactoring example 1: nested if conditions
%% Cell type:code id:d03bc22e tags: %% Cell type:code id:d03bc22e tags:
``` python ``` python
# check combination of a lock # check combination of a lock
def check_combination(a, b, c): def check_combination(a, b, c):
if a == 2: if a == 2:
if b == 2: if b == 2:
if c == 0: if c == 0:
return True return True
else: else:
return False return False
else: else:
return False return False
else: else:
return False return False
print(check_combination(2, 2, 0)) print(check_combination(2, 2, 0))
print(check_combination(2, 1, 0)) print(check_combination(2, 1, 0))
print(check_combination(1, 2, 0)) print(check_combination(1, 2, 0))
print(check_combination(3, 1, 9)) print(check_combination(3, 1, 9))
``` ```
%% Cell type:code id:feddb4b9 tags: %% Cell type:code id:feddb4b9 tags:
``` python ``` python
def bad_combo(a, b, c): def bad_combo(a, b, c):
pass pass
print(bad_combo(2, 2, 0)) print(bad_combo(2, 2, 0))
print(bad_combo(2, 1, 0)) print(bad_combo(2, 1, 0))
print(bad_combo(1, 2, 0)) print(bad_combo(1, 2, 0))
print(bad_combo(3, 1, 9)) print(bad_combo(3, 1, 9))
``` ```
%% Cell type:code id:b30eed5f tags: %% Cell type:code id:b30eed5f tags:
``` python ``` python
def refactor_combo(a, b, c): def refactor_combo(a, b, c):
pass pass
print(refactor_combo(2, 2, 0)) print(refactor_combo(2, 2, 0))
print(refactor_combo(2, 1, 0)) print(refactor_combo(2, 1, 0))
print(refactor_combo(1, 2, 0)) print(refactor_combo(1, 2, 0))
print(refactor_combo(3, 1, 9)) print(refactor_combo(3, 1, 9))
``` ```
%% Cell type:markdown id:8b7f186f tags: %% Cell type:markdown id:8b7f186f tags:
### Refactoring example 2: cluttered conditional ### Refactoring example 2: cluttered conditional
%% Cell type:code id:5285840d tags: %% Cell type:code id:5285840d tags:
``` python ``` python
def check_different(b1, b2): def check_different(b1, b2):
if b1 == True and b2 == False: if b1 == True and b2 == False:
return True return True
elif b1 == False and b2 == True: elif b1 == False and b2 == True:
return True return True
elif b1 == True and b2 == True: elif b1 == True and b2 == True:
return False return False
elif b1 == False and b2 == False: elif b1 == False and b2 == False:
return False return False
print(check_different(True, False)) print(check_different(True, False))
print(check_different(False, False)) print(check_different(False, False))
print(check_different(False, True)) print(check_different(False, True))
print(check_different(True, True)) print(check_different(True, True))
``` ```
%% Cell type:code id:e53c2cfe tags: %% Cell type:code id:e53c2cfe tags:
``` python ``` python
def refactor_check_different(b1, b2): def refactor_check_different(b1, b2):
pass pass
print(refactor_check_different(True, False)) print(refactor_check_different(True, False))
print(refactor_check_different(False, False)) print(refactor_check_different(False, False))
print(refactor_check_different(False, True)) print(refactor_check_different(False, True))
print(refactor_check_different(True, True)) print(refactor_check_different(True, True))
``` ```
%% Cell type:markdown id:5dba6585 tags: %% Cell type:markdown id:5dba6585 tags:
## After lecture ## After lecture
- go through examples in slides - go through examples in slides
- reason about every refactor version and predict correctness of the refactor - reason about every refactor version and predict correctness of the refactor
- try out each refactor example using Interactive Exercises - try out each refactor example using Interactive Exercises
- try these sample exam questions on refactoring (predict output, then type your code and run, to confirm your output) - try these sample exam questions on refactoring (predict output, then type your code and run, to confirm your output)
%% Cell type:code id:80c45d13 tags: %% Cell type:code id:80c45d13 tags:
``` python ``` python
# Refactoring Practice 1: recognizing equivalent code # Refactoring Practice 1: recognizing equivalent code
# Exam 1 Fall 2020 # Exam 1 Fall 2020
def g(x, y): def g(x, y):
# the expression after an if must be a Boolean expression or have a Boolean value # the expression after an if must be a Boolean expression or have a Boolean value
if x: if x:
if y: if y:
return True return True
else: else:
return False return False
else: else:
return False return False
# Which of the following will give the same result in all cases, as g(b1, b2)? # Which of the following will give the same result in all cases, as g(b1, b2)?
# a.) b1 != b2 # a.) b1 != b2
# b.) b1 and b2 # b.) b1 and b2
# c.) b1 == b2 # c.) b1 == b2
# d.) b1 or b2 # d.) b1 or b2
``` ```
%% Cell type:code id:795e83e4 tags: %% Cell type:code id:795e83e4 tags:
``` python ``` python
# Refactoring Practice 2: # Refactoring Practice 2:
def h(x, y): def h(x, y):
# the expression after an if must be a Boolean expression or have a Boolean value # the expression after an if must be a Boolean expression or have a Boolean value
if x: if x:
return False return False
else: else:
if y: if y:
return False return False
else: else:
return True return True
# Which of the following will give the same result in all cases, as h(b1, b2) ? # Which of the following will give the same result in all cases, as h(b1, b2) ?
# a.) b1 != b2 # a.) b1 != b2
# b.) b1 and b2 # b.) b1 and b2
# c.) b1 == b2 # c.) b1 == b2
# d.) not b1 and not b2 # d.) not b1 and not b2
``` ```
%% Cell type:code id:6fca19ec tags: %% Cell type:code id:6fca19ec tags:
``` python ``` python
# Refactoring Practice 3: # Refactoring Practice 3:
def some_bool_eval(x, y): def some_bool_eval(x, y):
# the expression after an if must be a Boolean expression or have a Boolean value # the expression after an if must be a Boolean expression or have a Boolean value
if x: if x:
return True return True
elif y: elif y:
return True return True
else: else:
return False return False
print(some_bool_eval(True, False)) print(some_bool_eval(True, False))
print(some_bool_eval(False, True)) print(some_bool_eval(False, True))
print(some_bool_eval(True, True)) print(some_bool_eval(True, True))
print(some_bool_eval(False, False)) print(some_bool_eval(False, False))
# what is the best way to refactor the body of the function ? # what is the best way to refactor the body of the function ?
# A. return x and y # A. return x and y
# B. return x or y # B. return x or y
# C. return x != y # C. return x != y
# D. return x == y # D. return x == y
``` ```
......
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