Control Flow Review Session - Stanford...
Transcript of Control Flow Review Session - Stanford...
![Page 1: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/1.jpg)
Control Flow Review Session
Will Kenney and Juliette Woodrow
![Page 2: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/2.jpg)
Todays ~Flow~
● if, if/else, and if/elif/else● while loops ● for loop variations● range function ● printing vs. returning● Top Down Decomposition● Incremental Testing
○ Doctests● Answer any of your questions ● Practice Problem
![Page 3: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/3.jpg)
![Page 4: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/4.jpg)
Control Flow Review
Thanks to Brahm Capoor for these awesome slides
![Page 5: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/5.jpg)
Control flow: the steps our program takes
if front_is_clear():# sick code here
![Page 6: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/6.jpg)
Control flow: the steps our program takes
if front_is_clear():# sick code here
if statements require a condition
![Page 7: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/7.jpg)
Control flow: the steps our program takes
if front_is_clear():# sick code here
conditions evaluate to True or False
![Page 8: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/8.jpg)
Control flow: the steps our program takes
if front_is_clear():# sick code here
An if statement will only execute if the condition
evaluates to True if front_is_clear == TRUE
![Page 9: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/9.jpg)
Control flow: the steps our program takes
if front_is_clear():# sick code here
If the condition is True, the code inside the if statement will
happen exactly once
![Page 10: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/10.jpg)
Control flow: the steps our program takes
if front_is_clear():# sick code here
# more sick code here
Once the code inside the if statement has completed, the program moves on, even if the
condition is still True
![Page 11: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/11.jpg)
Control flow: the steps our program takes
if front_is_clear():# sick code here
else: # different sick code here
![Page 12: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/12.jpg)
Control flow: the steps our program takes
if front_is_clear():# sick code here
else: # different sick code here
Sometimes we want to do one thing when a condition is True and something else when that
condition is False
![Page 13: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/13.jpg)
Control flow: the steps our program takes
if front_is_clear():# sick code here
elif beepers_present(): # other sick code here
else:# even more sick code here
![Page 14: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/14.jpg)
Control flow: the steps our program takes
if front_is_clear():# sick code here
elif beepers_present(): # other sick code here
else:# even more sick code here
Sometimes we want to do one thing when one condition is
True and something else when another that condition is True
![Page 15: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/15.jpg)
Control flow: the steps our program takes
if front_is_clear():# sick code here
elif beepers_present(): # other sick code here
elif beepers_not_present():# even more sick code here
Important Note: If you only use if/elifs, make sure you consider all cases.
![Page 16: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/16.jpg)
Control flow: the steps our program takes
while front_is_clear():# sick code here
![Page 17: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/17.jpg)
Control flow: the steps our program takes
while front_is_clear():# sick code here
while loops also require a condition, which
behaves in exactly the same way
![Page 18: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/18.jpg)
Control flow: the steps our program takes
while front_is_clear():# sick code here
The difference is that the while loop repeats so long as the condition is
True
![Page 19: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/19.jpg)
Control flow: the steps our program takes
While front_is_clear():# sick code here
# more sick code here
We only move on when the condition evaluates
to False
![Page 20: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/20.jpg)
Control flow: the steps our program takes
for i in range(42):# sick code here
![Page 21: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/21.jpg)
Control flow: the steps our program takes
for i in range(42):# sick code here
A for loop goes through each of the elements of
some collection of things
![Page 22: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/22.jpg)
Control flow: the steps our program takes
for i in range(42):# sick code here
The range function gives us an ordered collection of all the
non-negative integers below a particular number
![Page 23: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/23.jpg)
Control flow: the steps our program takes
for i in range(42):# sick code here
“Go through all the numbers until 42, one by one”
![Page 24: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/24.jpg)
Control flow: the steps our program takes
for pixel in image:# sick code here
“Go through all the pixels in image, one by one”
![Page 25: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/25.jpg)
Control flow: the steps our program takes
for pixel in image:# sick code here
# more sick code here
The for loop ends when we’ve gone through all the things in the
collection
![Page 26: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/26.jpg)
Other useful things to know about control flow
range(42) - all the numbers between 0 (inclusive) and 42 (exclusive)
range(10, 42) - all the numbers between 10 (inclusive) and 42 (exclusive)
range(10, 42, 2) - all the numbers between 10 (inclusive) and 42 (exclusive), going up by 2 each time
range(42, 10, -2) - all the numbers between 42 (inclusive) and 10(exclusive), going down by 2 each time.
![Page 27: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/27.jpg)
Printing vs Returning
Programs have a information flow, and a text output area, and those are separate.- When a function returns something, that’s information flowing out of the
function to another function- When a function prints something, that’s information being displayed on the
text output area (which is usually the terminal)
A useful metaphor is viewing a function as a painter inside a room- Returning is like the painter leaving the room and telling you something- Printing is like the painter hanging a painting inside the room- The painter can do either of those things without affecting whether they do the
other thing
Printing is sometimes described as a side effect, since it doesn’t directly influence the flow of information in a program
![Page 28: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/28.jpg)
Top Down Decomposition
● When faced with a new problem, we want to think about our large, overall problem by breaking it down into smaller and smaller problems○ Think about the milestones in the assignments!
● Think about making a cake: while the overall outcome is one, cohesive structure, there were various individual steps along the way○ The icing and the batter are made separately with their own unique
components and sub-steps (mixing in various ingredients at various times.)
○ When we code, we can see the end goal (red velvet cake!) but need to break down the problem into smaller, manageable subproblems.
![Page 29: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/29.jpg)
Top Down Decomposition
Think about our Ghost assignment....
![Page 30: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/30.jpg)
Top Down Decomposition
Think about our Ghost assignment....
Big goal: Create a new, unobstructed image
![Page 31: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/31.jpg)
Top Down Decomposition
Think about our Ghost assignment....
Big goal: Create a new, unobstructed image
Smaller goal: Find the ‘best’ pixel at a given (x, y)
![Page 32: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/32.jpg)
Top Down Decomposition
Think about our Ghost assignment....
Big goal: Create a new, unobstructed image
Smaller goal: Find the ‘best’ pixel at a given (x, y)
Smallest goal: Find the distance from a pixel to the average pixel at that (x, y)
![Page 33: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/33.jpg)
Top Down Decomposition
Think about our Ghost assignment....
Big goal: Create a new, unobstructed image
Smaller goal: Find the ‘best’ pixel at a given (x, y)
Smallest goal: Find the distance from a pixel to the average pixel at that (x, y)
TOP
![Page 34: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/34.jpg)
Incremental Testing
● Before moving on from one function to the next, you want to thoroughly test it
● This way, we can easily identify and eliminate any bugs caused by this function before using it in another function
● Python has a ~cool~ way to test individual functions called: doctests
![Page 35: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/35.jpg)
doctests
![Page 36: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/36.jpg)
doctestsdef average_minus_smallest(a,b,c):
“““ This function returns the difference btw the average value of a,b,c and the smallest value of a,b,c. ”””avg = (a+b+3)/3smallest = helper_func_for_min(a,b,c)return avg-smallest
![Page 37: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/37.jpg)
doctestsdef average_minus_smallest(a,b,c):
“““ This function returns the difference btw the average value of a,b,c and the smallest value of a,b,c. >>> average_minus_smallest(8,7,21)5”””avg = (a+b+3)/3smallest = helper_func_for_min(a,b,c)return avg-smallest
This is a doctest
![Page 38: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/38.jpg)
doctestsdef average_minus_smallest(a,b,c):
“““ This function returns the difference btw the average value of a,b,c and the smallest value of a,b,c. >>> average_minus_smallest(8,7,21)5”””avg = (a+b+3)/3smallest = helper_func_for_min(a,b,c)return avg-smallest
doctests help you test an individual function by running it on certain arguments that you provide and checking the return value.
![Page 39: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/39.jpg)
Composition of a doctestdef average_minus_smallest(a,b,c):
“““ This function returns the difference btw the average value of a,b,c and the smallest value of a,b,c. >>> average_minus_smallest(8,7,21)5”””avg = (a+b+3)/3smallest = helper_func_for_min(a,b,c)return avg-smallest
![Page 40: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/40.jpg)
Composition of a doctestdef average_minus_smallest(a,b,c):
“““ This function returns the difference btw the average value of a,b,c and the smallest value of a,b,c. >>> average_minus_smallest(8,7,21)5”””avg = (a+b+3)/3smallest = helper_func_for_min(a,b,c)return avg-smallest
Three right arrows followed by a space
![Page 41: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/41.jpg)
Composition of a doctestdef average_minus_smallest(a,b,c):
“““ This function returns the difference btw the average value of a,b,c and the smallest value of a,b,c. >>> average_minus_smallest(8,7,21)5”””avg = (a+b+3)/3smallest = helper_func_for_min(a,b,c)return avg-smallest
Name of the function
![Page 42: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/42.jpg)
Composition of a doctestdef average_minus_smallest(a,b,c):
“““ This function returns the difference btw the average value of a,b,c and the smallest value of a,b,c. >>> average_minus_smallest(8,7,21)5”””avg = (a+b+3)/3smallest = helper_func_for_min(a,b,c)return avg-smallest
Real arguments you want to use to test your function
![Page 43: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/43.jpg)
Composition of a doctestdef average_minus_smallest(a,b,c):
“““ This function returns the difference btw the average value of a,b,c and the smallest value of a,b,c. >>> average_minus_smallest(8,7,21)5”””avg = (a+b+3)/3smallest = helper_func_for_min(a,b,c)return avg-smallest
The return value you expect for those arguments.
![Page 44: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/44.jpg)
Composition of a doctestdef average_minus_smallest(a,b,c):
“““ This function returns the difference btw the average value of a,b,c and the smallest value of a,b,c. >>> average_minus_smallest(8,7,21)5”””avg = (a+b+3)/3smallest = helper_func_for_min(a,b,c)return avg-smallest
When you run this doctest, it will check if your program returns 5 when passed in 8, 7, and 21.
![Page 45: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/45.jpg)
You can have multiple doctests for a single function def average_minus_smallest(a,b,c):
“““ This function returns the difference btw the average value of a,b,c and the smallest value of a,b,c. >>> average_minus_smallest(8,7,21)5>>> average_minus_smallest(0,0,0)0”””avg = (a+b+3)/3smallest = helper_func_for_min(a,b,c)return avg-smallest
Use multiple doctests when there are multiple cases that you want to check.
![Page 46: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/46.jpg)
Tying it all together...
![Page 47: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/47.jpg)
Tying it all together...
● Use top down decomposition to break your program into smaller problems
![Page 48: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/48.jpg)
Tying it all together...
● Use top down decomposition to break your program into smaller problems
● Write a function for each problem
![Page 49: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/49.jpg)
Tying it all together...
● Use top down decomposition to break your program into smaller problems
● Write a function for each problem● Incrementally test as you write each function
○ AKA use doctests to ensure each function is bug-free before moving on to the next
![Page 50: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/50.jpg)
Tying it all together...
● Use top down decomposition to break your program into smaller problems
● Write a function for each problem● Incrementally test as you write each function
○ AKA use doctests to ensure each function is bug-free before moving on to the next
● Build your entire program
![Page 51: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/51.jpg)
Tying it all together...
● Use top down decomposition to break your program into smaller problems
● Write a function for each problem● Incrementally test as you write each function
○ AKA use doctests to ensure each function is bug-free before moving on to the next
● Build your entire program
● Become python master 🐍
![Page 52: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/52.jpg)
What questions do you have?
![Page 53: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/53.jpg)
Practice Problem: GCD
![Page 54: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/54.jpg)
Greatest Common Divisor
![Page 55: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/55.jpg)
Greatest Common Divisor
● Write a program that helps a user find the greatest common divisor of 3 numbers
![Page 56: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/56.jpg)
Greatest Common Divisor
● Write a program that helps a user find the greatest common divisor of 3 numbers
● GCD is the largest positive integer that divides each of the integers given
![Page 57: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/57.jpg)
Greatest Common Divisor
● Write a program that helps a user find the greatest common divisor of 3 numbers
● GCD is the largest positive integer that divides each of the integers given
● Your program should use helper functions to break this challenging task into smaller subproblems
![Page 58: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/58.jpg)
Greatest Common Divisor - Our Key Insights
![Page 59: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/59.jpg)
Greatest Common Divisor - Our Key Insights
● Breaking the problem down into smaller problems
![Page 60: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/60.jpg)
Greatest Common Divisor - Our Key Insights
● Breaking the problem down into smaller problems ○ Asking the user for 3 numbers ○ Compute the greatest common divisor○ Repeat these two tasks until the user enters SENTINEL value
![Page 61: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/61.jpg)
Greatest Common Divisor - Our Key Insights
● Breaking the problem down into smaller problems ○ Asking the user for 3 numbers ○ Compute the greatest common divisor○ Repeat these two tasks until the user enters SENTINEL value
● Break the program into functions
![Page 62: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/62.jpg)
Greatest Common Divisor - Our Key Insights
● Breaking the problem down into smaller problems ○ Asking the user for 3 numbers ○ Compute the greatest common divisor○ Repeat these two tasks until the user enters SENTINEL value
● Break the program into functions ○ get_user_input()
■ Asks users for 3 numbers and returns them ○ compute_gcd(a, b, c)
■ Input = 3 integers ■ Returns = the GCD of the 3 integers
○ main()■ Repeat those two steps while the user’s input != the SENTINEL
value
![Page 63: Control Flow Review Session - Stanford Universityweb.stanford.edu/class/cs106a/handouts/ControlFlowReview.pdf · Control Flow Review Session Will Kenney and Juliette Woodrow. Todays](https://reader033.fdocuments.net/reader033/viewer/2022050212/5f5e08aa08e5e414cc2f65d3/html5/thumbnails/63.jpg)