CS21 In Class: week 4

while loops, random library, functions

Setting up a subdirectory for week 4 in-class work

cd into your cs21/class/ subdirectory and create a new directory named 'week04', then cd into that directory:
$ cd cs21/class                     # cd into your cs21/class subdirectory
$ pwd	          
  /home/your_user_name/cs21/class

$ mkdir week04                      # make a subdirectory named week04   
$ cd week04                         # cd into it
$ pwd           
  /home/your_user_name/cs21/class/week04

Now copy over all the files from my public/cs21/week04 directory into your week04 directory (remember to add the dot as the destination of the cp command). From your week04 directory:

$ cp ~newhall/public/cs21/week04/* .
$ ls
example_funcs.py  infiniteloop.py  squares.py       syracuse.py
factorial.py      randOps.py       squares_list.py
format.py         simple_while.py  stars.py

Weekly In-class Examples

In Tuesday's class we are going to try out some programs with while loops and learn the random library. We will look at some of the following:

  1. format.py: some examples of string formating for nicer ouput stmts

  2. simple_while.py: an example while loop

  3. infiniteloop.py: runs forever. stop it by type CNTL-C

  4. syracuse.py: try solving the syracuse sequence problem

  5. randomOps.py: examples using the randrange function from the random library

  6. stars.py: try solving some of these

In Thursday's class we are going to learn about functions. We will look at some of the following:

  1. factorial.py: writing our own function

  2. squares.py: another example

  3. example_funcs.py: some different types of functions

  4. squares_list.py: a function with a list parameter

  5. create a new function in example_funcs.py named get_single_char that prompts user to enter a single character and returns valid input to the caller. It should not return until the user enters just a single character input value. Replace a call to it in this function where it reads in the letter. Questions:
    • Does this function need any input values?
      If so, how many? what type are they?
    • Does this function return a value? If so, what type?
    • Do we need a loop? for or while? What is condition?