conditionals control flow announcements for this lecture
play

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

Lecture 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 Request


  1. Lecture 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 today 9/19/19 Conditionals & Program Flow 2

  3. Testing last_name_first(n) # test procedure Call function def test_last_name_first(): on test input """Test procedure for last_name_first(n)""" result = name.last_name_first('Walker White') Compare to expected output cornell.assert_equals('White, Walker', result) result = name.last_name_first('Walker White') cornell.assert_equals('White, Walker', result) Call test procedure # Script code to activate the test test_last_name_first() print('Module name passed all tests.') 9/19/19 Conditionals & Program Flow 3

  4. Types of Testing Black Box Testing White Box Testing • Function is “opaque” • Function is “transparent” § Test looks at what it does § Tests/debugging takes place inside of function § Fruitful : what it returns § Focuses on where error is § Procedure : what changes • Example : Use of print • Example : Unit tests • Problems : • Problems : § Much harder to do § Are the tests everything? § Must remove when done § What caused the error? 9/19/19 Conditionals & Program Flow 4

  5. Types of Testing Black Box Testing White Box Testing • Function is “opaque” • Function is “transparent” § Test looks at what it does § Tests/debugging takes Can actually place inside of function Works on § Fruitful : what it returns § Focuses on where error is find the bug § Procedure : what changes functions you • Example : Use of print in function • Example : Unit tests did not define • Problems : • Problems : § Much harder to do § Are the tests everything? § Must remove when done § What caused the error? 9/19/19 Conditionals & Program Flow 5

  6. Finding the Error • Unit tests cannot find the source of an error • Idea: “Visualize” the program with print statements def last_name_first(n): """Returns: copy of n in form 'last-name, first-name' """ end_first = n.find(' ') Print variable after print(end_first) each assignment first = n[:end_first] print('first is '+str(first)) Optional : Annotate last = n[end_first+1:] value to make it easier to identify print('last is '+str(last)) return last+', '+first 9/19/19 Conditionals & Program Flow 6

  7. How to Use the Results • Goal of white box testing is error location § Want to identify the exact line with the error § Then you look real hard at line to find error § What you are doing in lab this week • But similar approach to black box testing § At each line you have expected print result § Compare it to the received print result § Line before first mistake is likely the error 9/19/19 Conditionals & Program Flow 7

  8. Warning About Print Statements • Must remove them when you are done § Not part of the specification (violation) § Slow everything down unnecessarily § App Store will reject an app with prints • But you might want them again later § Solution : “comment them out” § You can always uncomment later 9/19/19 Conditionals & Program Flow 8

  9. Structure vs. Flow Program Structure Program Flow • Order code is presented • Order code is executed § Order statements are listed § Not the same as structure § Inside/outside of function § Some statements duplicated § Will see other ways… § Some statements skipped • Defines possibilities over • Defines what happens in a multiple executions single execution Have already seen this difference with functions 9/19/19 Conditionals & Program Flow 9

  10. Structure vs. Flow: Example Program Structure Program Flow def foo(): > python foo.py print('Hello') 'Hello' Statement 'Hello' Statement executed 3x listed once 'Hello' # Script Code foo() Bugs occur when flow does foo() not match expectations foo() 9/19/19 Conditionals & Program Flow 10

  11. Conditionals: If-Statements Format Example if expression : # Put x in z if it is positive statement if x > 0: … z = x statement Indent Execution : If expression is True , execute all statements indented underneath 9/19/19 Conditionals & Program Flow 11

  12. Python Tutor Example 9/19/19 Conditionals & Program Flow 12

  13. Conditionals: If-Else-Statements Format Example if expression : # Put max of x, y in z statement if x > y: … z = x else: else: statement z = y … Execution : If expression is True , execute all statements indented under if. If expression is False , execute all statements indented under else. 9/19/19 Conditionals & Program Flow 13

  14. Python Tutor Example 9/19/19 Conditionals & Program Flow 14

  15. Conditionals: “Control Flow” Statements b Branch Point: if b : Evaluate & Choose s1 # statement s1 s3 s3 Statement: Execute if b : b s1 Flow else: Program only s1 s2 takes one path s2 each execution s3 s3 9/19/19 Conditionals & Program Flow 15

  16. Program Flow and Call Frames def max(x,y): max(0,3) : """Returns: max of x, y""" # simple implementation max 1 1 if x > y: x 0 2 return x y 3 3 return y Frame sequence depends on flow 9/19/19 Conditionals & Program Flow 16

  17. Program Flow and Call Frames def max(x,y): max(0,3) : """Returns: max of x, y""" # simple implementation max 3 1 if x > y: x 0 2 return x y 3 3 return y Frame sequence Skips line 2 depends on flow 9/19/19 Conditionals & Program Flow 17

  18. Program Flow and Call Frames def max(x,y): max(0,3) : """Returns: max of x, y""" # simple implementation max 1 if x > y: x 0 RETURN 2 return x y 3 3 3 return y Frame sequence Skips line 2 depends on flow 9/19/19 Conditionals & Program Flow 18

  19. Program Flow vs. Local Variables • max(3,0) : def max(x,y): """Returns: max of x, y""" max 1 # swap x, y x y 3 0 # put the larger in y 1 if x > y: 2 temp = x 3 x = y 4 y = temp Swaps max into var y 5 return y 9/19/19 Conditionals & Program Flow 19

  20. Program Flow vs. Local Variables • max(3,0) : def max(x,y): """Returns: max of x, y""" max 2 # swap x, y x y 3 0 # put the larger in y 1 if x > y: 2 temp = x 3 x = y 4 y = temp Swaps max into var y 5 return y 9/19/19 Conditionals & Program Flow 20

  21. Program Flow vs. Local Variables • max(3,0) : def max(x,y): """Returns: max of x, y""" max 3 # swap x, y x y 3 0 # put the larger in y 1 if x > y: temp 3 2 temp = x 3 x = y 4 y = temp Swaps max into var y 5 return y 9/19/19 Conditionals & Program Flow 21

  22. Program Flow vs. Local Variables • max(3,0) : def max(x,y): """Returns: max of x, y""" max 4 # swap x, y x y 0 0 # put the larger in y 1 if x > y: temp 3 2 temp = x 3 x = y 4 y = temp Swaps max into var y 5 return y 9/19/19 Conditionals & Program Flow 22

  23. Program Flow vs. Local Variables • max(3,0) : def max(x,y): """Returns: max of x, y""" max 5 # swap x, y x y 0 3 # put the larger in y 1 if x > y: temp 3 2 temp = x 3 x = y 4 y = temp Swaps max into var y 5 return y 9/19/19 Conditionals & Program Flow 23

  24. Program Flow vs. Local Variables • max(3,0) : def max(x,y): """Returns: max of x, y""" max # swap x, y x y 0 3 # put the larger in y 1 if x > y: temp 3 2 temp = x 3 RETURN 3 x = y 4 y = temp Swaps max into var y 5 return y 9/19/19 Conditionals & Program Flow 24

  25. Program Flow vs. Local Variables • Value of max(3,0) ? def max(x,y): """Returns: max of x, y""" A: 3 # swap x, y B: 0 # put the larger in y C: Error! 1 if x > y: D: I do not know 2 temp = x 3 x = y 4 y = temp 5 return temp 9/19/19 Conditionals & Program Flow 25

  26. Program Flow vs. Local Variables • Value of max(3,0) ? def max(x,y): """Returns: max of x, y""" A: 3 CORRECT # swap x, y B: 0 # put the larger in y C: Error! 1 if x > y: D: I do not know 2 temp = x 3 x = y • Local variables last until 4 y = temp § They are deleted or § End of the function 5 return temp • Even if defined inside if 9/19/19 Conditionals & Program Flow 26

  27. Program Flow vs. Local Variables • Value of max(0,3) ? def max(x,y): """Returns: max of x, y""" A: 3 # swap x, y B: 0 # put the larger in y C: Error! 1 if x > y: D: I do not know 2 temp = x 3 x = y 4 y = temp 5 return temp 9/19/19 Conditionals & Program Flow 27

Recommend


More recommend