In Class: Week 6, Tuesday
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
$ ls
fivequestions.py grades.py squareevens.py syracuse.py
We are going to do some of the following together in class:
Using functions with objects
- First, we will open up drawshapes.py from last week (week05 directory)
and use functions to simplify the repetitive process of getting user mouse
clicks. On your own, write another function (drawTriangle) that further
simplifies the task of creating a triangle multiple times.
Practice with functions with mutable object parameters:
- 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
- Open up grades.py. This is the solution to the first problem in lab 3.
In the lab, we assumed the user gave valid input (i.e., a number between 0
and 100). I have added 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
- 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 CNTRL and the
c keys (CNTL-C).