While loops

In class exercises
Getting started
Create a w06-while subdirectory in your cs21/class directory. Then, from within your w06-while subdirectory copy over some python files from my public directory:
[~]$ cd cs21/inclass 
[inclass]$ pwd
/home/your_user_name/cs21/inclass

[inclass]$ mkdir w06-while        
[inclass]$ cd w06-while
[w06-while]$ pwd
/home/your_user_name/cs21/inclass/w06-while

[w06-while]$ cp ~adanner/public/cs21/w06-while/* .
[w06-while]$ ls
fivequestions.py  randOps.py      stringOps.py  whileLoop.py
listOps.py        squareevens.py  syracuse.py
[w06-while]$ 
While loops
We want to spend the most of the week talking about while loops which combine elements of a for loop and an if statement. We are going to do some of the following together in class:
  1. Practice with while loops
    1. open syracuse.py in vim. We will finish the code in main to compute the Syracuse sequence for an integer value entered by the user.
    2. open fivequestions.py. We will write the getYesOrNo function together.
  2. strings and lists as objects, and the random library:
    On the class schedule for week6 is a link to information about using strings and lists as objects, and about using the random library. Read through this, and look at the sample code that you copied over from my w06-while subdirectoy. We will talk about using lists and strings as objects in class, but you may want to look at this and try out some of the example code you copied over (stringOps.py, listOps.py, randomOps.py).
  3. Some practice with functions with list and object parameters:
    open squareevens.py.
    1. We are going to add two functions: printList, that takes a list of integers and prints it to the terminal; and squareEvens that takes a list of integers and squares all the even elements in the list.
    2. Now let's change squareEvens so that it returns the number of elements it changed in the list.
    3. Now let's add a new function, createRandomList, to squareevens.py. This is a function that will return a list of some some number of elements between 10 and 20 (use the random library to pick the number of elements value), where each element in the list is some value between 0 and 1000 (again, use the random library to pick the value of each item you add to the list).
      Next, let's change the main function to make a call to our new function to create the list, main should print the list out, call squareEvens, and then print out the list again.
Strings and lists as objects
We will be using strings and lists as objects more frequently now. Please read over the notes on this topic and let me know if you have questions.

The random number module
Read over the Random library documentation and experiment with randOps.py