CS170: Programming for the World Around Us - Iteration with Python

Activity Goals

The goals of this activity are:
  1. To be able to explain the uses of the while loop structure
  2. To be able to apply boolean expressions to iterative structures via the while loop
  3. To be able to explain the uses of the for loop structure
  4. 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

  1. Modify this code to implement a checkIfRaining() function that generates a random number between 1 and 10, and returns true if the number is greater than 7 (and return false otherwise).
  2. Modify this program to ensure that the number of minutes is never 0 (make it at least 1).
  3. 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

  1. The code prints the numbers from 0 through 9. Why doesn’t it also print the value 10?
  2. What could you do to change this to print the values 0 through 10?
  3. What could you do to print the values 1 through 10?

Model 3: Nested Loops

Questions

  1. How many times does the inner loop print statement execute?
  2. How many times does the outer loop print statement execute?
  3. How many times does the first print statement execute, and why?

Model 4: Flipping a Coin

Questions

  1. What does range(10) do? Try printing range(10) to the screen; how does it work?
  2. Rewrite this program to use a for loop instead.
  3. Modify this program to only print the number of heads once after the loop has finished.
  4. How many times would you expect to land on heads?
  5. What would you change to flip the coin 100 times? How many heads would you expect then?
  6. 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

  1. What does this program do?
  2. Modify this program to count the number of vowels in the string, and print out the result.

Submission

I encourage you to submit your answers to the questions (and ask your own questions!) using the Class Activity Questions discussion board. You may also respond to questions or comments made by others, or ask follow-up questions there. Answer any reflective prompt questions in the Reflective Journal section of your OneNote Classroom personal section. You can find the link to the class notebook on the syllabus.