Conditionals & Control Flow Announcements For This Lecture - - PowerPoint PPT Presentation

conditionals control flow announcements for this lecture
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Conditionals & Control Flow

Presentation 7

slide-2
SLIDE 2

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

slide-3
SLIDE 3

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
slide-4
SLIDE 4

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

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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?

slide-7
SLIDE 7

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

slide-8
SLIDE 8

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:

¯\_(ツ)_/¯

slide-9
SLIDE 9

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?

slide-10
SLIDE 10

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

slide-11
SLIDE 11

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?

slide-12
SLIDE 12

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

slide-13
SLIDE 13

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

slide-14
SLIDE 14

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?

slide-15
SLIDE 15

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

slide-16
SLIDE 16

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?

slide-17
SLIDE 17

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

slide-18
SLIDE 18

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?

slide-19
SLIDE 19

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
slide-20
SLIDE 20

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

slide-21
SLIDE 21

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?

slide-22
SLIDE 22

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

slide-23
SLIDE 23

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?

slide-24
SLIDE 24

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

slide-25
SLIDE 25

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!

slide-26
SLIDE 26

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

slide-27
SLIDE 27

9/24/20 Conditionals & Program Flow 27

Questions?

slide-28
SLIDE 28

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

slide-29
SLIDE 29

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