conditionals control flow announcements for this lecture
play

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


  1. Presentation 7 Conditionals & Control Flow

  2. 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 § Request your partner • Due Wednesday at mid. § Other person accepts § Can work at it during lab AI Quiz § But labs are due as normal • One-on-Ones ongoing • Sent out several e-mails § Lots of spaces available • Will start dropping Tues 9/24/20 Conditionals & Program Flow 2

  3. 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 Video Lessons § Request your partner • Due Wednesday at mid. § Other person accepts • Lesson 9 for today § Can work at it during lab AI Quiz • Lesson 10 for next time § But labs are due as normal • One-on-Ones ongoing • Sent out several e-mails § Lots of spaces available • Will start dropping Tues 9/24/20 Conditionals & Program Flow 3

  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

  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 Conditionals & Program Flow 5

  6. A Simple Function Function Definition Function Call def sign(n): >>> x = sign(4) """Returns “sign” n.""” 13 … if n > 0: 20 What does the return 1 21 frame look like elif n < 0: 22 at the start ? return -1 23 else: 24 return 0 25 9/24/20 Conditionals & Program Flow 6

  7. Which One is Closest to Your Answer? A: B: sign sign 13 20 n 4 n 4 C: D: sign sign 13 21 n 4 9/24/20 Conditionals & Program Flow 7

  8. Which One is Closest to Your Answer? A: B: sign sign 13 20 n 4 n 4 E: ¯\_( ツ )_/¯ C: D: sign sign 13 21 n 4 9/24/20 Conditionals & Program Flow 8

  9. A Simple Function Function Definition Function Call def sign(n): >>> x = sign(4) """Returns “sign” n.""” 13 B: … sign 20 if n > 0: 20 return 1 21 n 4 elif n < 0: 22 return -1 23 else: 24 What is the next step ? return 0 25 9/24/20 Conditionals & Program Flow 9

  10. Which One is Closest to Your Answer? A: B: sign sign 21 n 4 n 4 1 1 RETURN RETURN C: D: sign sign 21 n 4 n 4 1 RETURN 9/24/20 Conditionals & Program Flow 10

  11. A Simple Function Function Definition Function Call def sign(n): >>> x = sign(4) """Returns “sign” n.""” 13 D: … sign 21 if n > 0: 20 return 1 21 n 4 elif n < 0: 22 return -1 23 else: 24 What is the next step ? return 0 25 9/24/20 Conditionals & Program Flow 11

  12. Which One is Closest to Your Answer? A: B: sign sign 21 n 4 n 4 1 1 RETURN RETURN C: D: sign x 1 n 4 sign 4 n 1 RETURN 1 RETURN 9/24/20 Conditionals & Program Flow 12

  13. A Simple Function Function Definition Function Call def sign(n): >>> x = sign(4) """Returns “sign” n.""” 13 A: … sign if n > 0: 20 return 1 21 n 4 elif n < 0: 22 1 RETURN return -1 23 else: 24 return 0 25 9/24/20 Conditionals & Program Flow 13

  14. Let’s Try This Again Function Definition Function Call def sign(n): >>> x = sign(-3) """Returns “sign” n.""” 13 … if n > 0: 20 What does the return 1 21 frame look like elif n < 0: 22 at the start ? return -1 23 else: 24 return 0 25 9/24/20 Conditionals & Program Flow 14

  15. Which One is Closest to Your Answer? A: B: sign sign 13 20 n -3 n -3 C: D: sign sign 22 23 n -3 n -3 9/24/20 Conditionals & Program Flow 15

  16. Let’s Try This Again Function Definition Function Call def sign(n): >>> x = sign(-3) """Returns “sign” n.""” 13 B: … sign 20 if n > 0: 20 return 1 21 n -3 elif n < 0: 22 return -1 23 else: 24 What is the next step ? return 0 25 9/24/20 Conditionals & Program Flow 16

  17. Which One is Closest to Your Answer? A: B: sign sign 22 n -3 n -3 - 1 - 1 RETURN RETURN C: D: sign sign 22 23 n -3 n -3 9/24/20 Conditionals & Program Flow 17

  18. Let’s Try This Again Function Definition Function Call def sign(n): >>> x = sign(-3) """Returns “sign” n.""” 13 C: … sign 22 if n > 0: 20 return 1 21 n -3 elif n < 0: 22 return -1 23 else: 24 What is the next step ? return 0 25 9/24/20 Conditionals & Program Flow 18

  19. Which One is Closest to Your Answer? A: B: sign sign 23 23 n -3 n -3 - 1 RETURN C: D: sign sign n -3 n -3 - 1 - 1 RETURN RETURN 9/24/20 Conditionals & Program Flow 19

  20. Let’s Try This Again Function Definition Function Call def sign(n): >>> x = sign(-3) """Returns “sign” n.""” 13 A: … sign 23 if n > 0: 20 return 1 21 n -3 elif n < 0: 22 return -1 23 Only thing changing else: 24 is Instruction Counter return 0 25 9/24/20 Conditionals & Program Flow 20

  21. One Last Time Function Definition Function Call def sign(n): >>> x = sign(0) """Returns “sign” n.""” 13 You start with: … sign if n > 0: 20 20 return 1 21 n 0 elif n < 0: 22 return -1 23 else: 24 What is the next step ? return 0 25 9/24/20 Conditionals & Program Flow 21

  22. Which One is Closest to Your Answer? A: B: sign sign 24 22 n 0 n 0 C: D: sign sign 25 23 n 0 n 0 9/24/20 Conditionals & Program Flow 22

  23. One Last Time Function Definition Function Call def sign(n): >>> x = sign(0) """Returns “sign” n.""” 13 B: … sign 22 if n > 0: 20 return 1 21 n 0 elif n < 0: 22 return -1 23 else: 24 What is the next step ? return 0 25 9/24/20 Conditionals & Program Flow 23

  24. Which One is Closest to Your Answer? A: B: sign sign 24 n 0 n 0 0 RETURN C: D: sign sign 25 25 n 0 n 0 0 RETURN 9/24/20 Conditionals & Program Flow 24

  25. One Last Time Function Definition Function Call def sign(n): >>> x = sign(0) """Returns “sign” n.""” 13 D: … sign 25 if n > 0: 20 return 1 21 n 0 elif n < 0: 22 return -1 23 else: 24 else is not executed! return 0 25 9/24/20 Conditionals & Program Flow 25

  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

  27. Questions? 9/24/20 Conditionals & Program Flow 27

  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 Specifications & Testing 28

  29. How Many Valid, Different Tests? Input Output 'aeiou' '' A: 2 'heat' 'ht' B: 3 'bather' 'bthr' C: 4 'sky' 'sk' D: 5 'Ashen' 'shn' E: 6 'a12' '12' 15.0 ERROR 9/22/20 Specifications & Testing 29

Recommend


More recommend