In Class: Week 6, Tuesday, Thursday


Run update21 to create a week06 subdirectory and copy example programs to your cs21/class directory. From there, move to your week06 directory:

    $ update21
    $ cd 
    $ cd cs21/class/week06
    $ pwd
      /home/your_user_name/cs21/class/week06
	

Topics


We are going to do some of the following together in class:

    Practice with functions with mutable object parameters:

  1. In squareevens.py, we examine the effect of handing a mutable object off to a function. Specifically, we will implement a function that takes in a list of numbers and squares all of the even values. Next, trace through the code to see how modifying mutable data types affects the original argument in the main() function.

    Error checking user input using while loops

  2. Open up grades.py. This is the solution to an in-class problem early in the semester. In our solution, we assumed the user gave valid input (i.e., a number between 0 and 100). We'll add error checking to the solution by outputting an error message to the screen if the user does not provide a valid number. Once you are familiar with while loops, modify this code to ask the user to reenter the score until they provide a valid number.

    Practice with while loops

  3. open up coinflip.py. Previously we learned how to simulate a coin flip using the random library. Here, write a function to return one coin flip (represent a "head" as the value True). Next, write a while-loop in main() that flips a coin multiple times until a total of 10 heads appear. Output how many total flips it took to reach 10 heads (HINT: you'll need two accumulator variables).
  4. open syracuse.py in vim. We will implement a function syracuse, that takes a starting value and computes the Syracuse sequence for the value (printing out each term that is computed).

    To kill a program with an infinite loop, hold down the CTRL and the c keys (i.e., CTRL-C).