CS170: Programming for the World Around Us - Iteration with Python
Activity Goals
The goals of this activity are:- To be able to explain the uses of the
while
loop structure - To be able to apply boolean expressions to iterative structures via the
while
loop - To be able to explain the uses of the
for
loop structure - To be able to apply boolean expressions to iterative structures via the
for
loop
Supplemental Reading
Feel free to visit these resources for supplemental background reading material.The Activity
Directions
Consider the activity models and answer the questions provided. First reflect on these questions on your own briefly, before discussing and comparing your thoughts with your group. Appoint one member of your group to discuss your findings with the class, and the rest of the group should help that member prepare their response. Answer each question individually from the activity, and compare with your group to prepare for our whole-class discussion. After class, think about the questions in the reflective prompt and respond to those individually in your notebook. Report out on areas of disagreement or items for which you and your group identified alternative approaches. Write down and report out questions you encountered along the way for group discussion.Model 1: The while
Loop
Conditionals can be used to repeatedly execute code. There are two varieties of these "loops:" the for loop (which is useful when counting the number of iterations that are needed), and the while loop (which is useful for executing until something is true).
Questions
- Modify this code to implement a
checkIfRaining()
function that generates a random number between 1 and 10, and returnstrue
if the number is greater than 7 (and returnfalse
otherwise). - Modify this program to ensure that the number of minutes is never 0 (make it at least 1).
- Write a program to ask the user for their age, but keep asking until they enter a positive number.
Model 2: The for
Loop
Questions
- The code prints the numbers from 0 through 9. Why doesn’t it also print the value 10?
- What could you do to change this to print the values 0 through 10?
- What could you do to print the values 1 through 10?
Model 3: Nested Loops
Questions
- How many times does the inner loop print statement execute?
- How many times does the outer loop print statement execute?
- How many times does the first print statement execute, and why?
Model 4: Flipping a Coin
Questions
- What does
range(10)
do? Try printingrange(10)
to the screen; how does it work? - Rewrite this program to use a
for
loop instead. - Modify this program to only print the number of heads once after the loop has finished.
- How many times would you expect to land on heads?
- What would you change to flip the coin 100 times? How many heads would you expect then?
- What would be the effect of changing the constant in the program from 0.5 to 0.3?
Model 5: Iterating over Text Characters
Questions
- What does this program do?
- Modify this program to count the number of vowels in the string, and print out the result.