Conditionals & Control Flow Announcements For This Lecture - - PowerPoint PPT Presentation
Conditionals & Control Flow Announcements For This Lecture - - PowerPoint PPT Presentation
Presentation 7 Conditionals & Control Flow Announcements For This Lecture Assignment 1 Partners Should be working on it You must pair in CMS Have covered everything Go into the submission Look at lab for more help
Announcements For This Lecture Assignment 1
- Should be working on it
§ Have covered everything § Look at lab for more help
- Due Wednesday at mid.
§ Can work at it during lab § But labs are due as normal
- One-on-Ones ongoing
§ Lots of spaces available
Partners
- You must pair in CMS
- Go into the submission
§ Request your partner § Other person accepts
- Sent out several e-mails
- Will start dropping Tues
2 9/24/20 Conditionals & Program Flow
AI Quiz
Announcements For This Lecture Assignment 1
- Should be working on it
§ Have covered everything § Look at lab for more help
- Due Wednesday at mid.
§ Can work at it during lab § But labs are due as normal
- One-on-Ones ongoing
§ Lots of spaces available
Partners
- You must pair in CMS
- Go into the submission
§ Request your partner § Other person accepts
- Sent out several e-mails
- Will start dropping Tues
3 9/24/20 Conditionals & Program Flow
AI Quiz
Video Lessons
- Lesson 9 for today
- Lesson 10 for next time
About The Current Lab
- Has you write functions with conditionals
§ Technically (a little) harder than A1 § Historically it was held after A1 was due
- Compromise: Due next Thurs/Fri
§ Have a week to work on it (and do A1 first) § If you are struggling get help in next lab
- No new lab covered next Tues/Wed
§ Time in class to work on assignment § Or to get help on conditionals lab
9/24/20 Conditionals & Program Flow 4
A Simple Function
def sign(n): """ Returns the “sign” of the number n. The sign is 1 if n > 0, -1 if n < 0 and 0 if n == 0. Parameter n: the number to check Precondition: n is a number (int or float) """
9/24/20 5 Conditionals & Program Flow
A Simple Function
Function Definition
def sign(n): """Returns “sign” n.""” … if n > 0: return 1 elif n < 0: return -1 else: return 0
Function Call >>> x = sign(4)
9/24/20 Conditionals & Program Flow 6
22 23 24 21 20 13 25
What does the frame look like at the start?
Which One is Closest to Your Answer?
A: B:
9/24/20 Conditionals & Program Flow 7
C: D:
sign 13 sign 21 4 n sign 13 4 n sign 20 4 n
Which One is Closest to Your Answer?
A: B:
9/24/20 Conditionals & Program Flow 8
C: D:
sign 13 sign 21 4 n sign 13 4 n sign 20 4 n
E:
¯\_(ツ)_/¯
A Simple Function
Function Definition
def sign(n): """Returns “sign” n.""” … if n > 0: return 1 elif n < 0: return -1 else: return 0
Function Call >>> x = sign(4) B:
9/24/20 Conditionals & Program Flow 9
22 23 24 21 20 13 25 sign 20 4 n
What is the next step?
Which One is Closest to Your Answer?
A: B:
9/24/20 Conditionals & Program Flow 10
C: D:
sign sign 21 4 n sign 4 n sign 21 4 n
RETURN
1
RETURN
1 4 n
RETURN
1
A Simple Function
Function Definition
def sign(n): """Returns “sign” n.""” … if n > 0: return 1 elif n < 0: return -1 else: return 0
Function Call >>> x = sign(4) D:
9/24/20 Conditionals & Program Flow 11
22 23 24 21 20 13 25 sign 21 4 n
What is the next step?
Which One is Closest to Your Answer?
A: B:
9/24/20 Conditionals & Program Flow 12
C: D:
sign sign 4 n sign 21 4 n
RETURN
1
RETURN
1 4 n
RETURN
1
sign 4 n
RETURN
1
1 x
A Simple Function
Function Definition
def sign(n): """Returns “sign” n.""” … if n > 0: return 1 elif n < 0: return -1 else: return 0
Function Call >>> x = sign(4) A:
9/24/20 Conditionals & Program Flow 13
22 23 24 21 20 13 25 sign 4 n
RETURN
1
Let’s Try This Again
Function Definition
def sign(n): """Returns “sign” n.""” … if n > 0: return 1 elif n < 0: return -1 else: return 0
Function Call >>> x = sign(-3)
9/24/20 Conditionals & Program Flow 14
22 23 24 21 20 13 25
What does the frame look like at the start?
Which One is Closest to Your Answer?
A: B:
9/24/20 Conditionals & Program Flow 15
C: D:
sign 22 sign 23
- 3
n sign 13
- 3
n sign 20
- 3
n
- 3
n
Let’s Try This Again
Function Definition
def sign(n): """Returns “sign” n.""” … if n > 0: return 1 elif n < 0: return -1 else: return 0
Function Call >>> x = sign(-3) B:
9/24/20 Conditionals & Program Flow 16
22 23 24 21 20 13 25 sign 20
- 3
n
What is the next step?
Which One is Closest to Your Answer?
A: B:
9/24/20 Conditionals & Program Flow 17
C: D:
sign 22 sign 23
- 3
n sign
- 3
n sign 22
- 3
n
RETURN
- 1
RETURN
- 1
- 3
n
Let’s Try This Again
Function Definition
def sign(n): """Returns “sign” n.""” … if n > 0: return 1 elif n < 0: return -1 else: return 0
Function Call >>> x = sign(-3) C:
9/24/20 Conditionals & Program Flow 18
22 23 24 21 20 13 25 sign 22
- 3
n
What is the next step?
Which One is Closest to Your Answer?
A: B:
9/24/20 Conditionals & Program Flow 19
C: D:
sign sign
- 3
n sign 23
- 3
n sign 23
- 3
n
RETURN
- 1
RETURN
- 1
- 3
n
RETURN
- 1
Let’s Try This Again
Function Definition
def sign(n): """Returns “sign” n.""” … if n > 0: return 1 elif n < 0: return -1 else: return 0
Function Call >>> x = sign(-3) A:
9/24/20 Conditionals & Program Flow 20
22 23 24 21 20 13 25 sign 23
- 3
n
Only thing changing is Instruction Counter
One Last Time
Function Definition
def sign(n): """Returns “sign” n.""” … if n > 0: return 1 elif n < 0: return -1 else: return 0
Function Call >>> x = sign(0) You start with:
9/24/20 Conditionals & Program Flow 21
22 23 24 21 20 13 25 sign 20 n
What is the next step?
Which One is Closest to Your Answer?
A: B:
9/24/20 Conditionals & Program Flow 22
C: D:
sign 25 sign 23 n sign 24 n sign 22 n n
One Last Time
Function Definition
def sign(n): """Returns “sign” n.""” … if n > 0: return 1 elif n < 0: return -1 else: return 0
Function Call >>> x = sign(0) B:
9/24/20 Conditionals & Program Flow 23
22 23 24 21 20 13 25 sign 22 n
What is the next step?
Which One is Closest to Your Answer?
A: B:
9/24/20 Conditionals & Program Flow 24
C: D:
sign 25 sign 25 n sign n sign 24 n
RETURN
n
RETURN
One Last Time
Function Definition
def sign(n): """Returns “sign” n.""” … if n > 0: return 1 elif n < 0: return -1 else: return 0
Function Call >>> x = sign(0) D:
9/24/20 Conditionals & Program Flow 25
22 23 24 21 20 13 25 sign 25 n
else is not executed!
Bonus Question Write sign(n) as a conditional expression
- A. 1 if x > 0, -1 elif x < 0, else 0
- B. if x > 0 then 1 elif x < 0 then -1 else 0
- C. 1 if x > 0 else (-1 if x < 0 else 0)
- D. (1 if x > 0 else -1) if x < 0 else 0
- E. What is a conditional expression?
9/24/20 Conditionals & Program Flow 26
9/24/20 Conditionals & Program Flow 27
Questions?
Recall: Designing Tests
def disemvowel (s): """Returns a copy of s with vowels removed Vowels are a, e, i, o, and u. y is not a vowel Example: disemvowel('boat') returns 'bt' Parameter s: a string to disemvowel Precondition: s is a nonempty string of lowercase letters"""
9/22/20 28 Specifications & Testing
How Many Valid, Different Tests?
Input Output 'aeiou' '' 'heat' 'ht' 'bather' 'bthr' 'sky' 'sk' 'Ashen' 'shn' 'a12' '12' 15.0 ERROR
9/22/20 29
A: 2 B: 3 C: 4 D: 5 E: 6
Specifications & Testing