While loops

In class exercises
Announcements

Labs in this course are designed to be completed using the skills and tools we discuss in class. You shouldn't need to google for advanced topics, read ahead in the book, or ask a CS major. You can ask for general help using these resources, but be sure to cite them as a comment in your lab. Often, using an advanced feature of python does not stress the core problem solving skills the lab is designed to strengthen. Yes s[::-1] reverses a string in python, but attempting this using a string accumulator will improve your problem solving skills.

In CS21, you must complete labs submitted for a grade on your own. You may not email, print, or otherwise show lab code to another student. You may not write code for another student or have anyone write code for you. You may not review another student's lab code prior to the deadline. In upper level courses, you are encouraged to work with one or more partners on collaborative assignments, but in CS21, we need to ensure that individuals are capable of problem solving on their own.

During in class assignments, and quiz studying, you are encouraged to collaborate and share ideas, but you this collaboration is prohibited on material submitted for a grade.

Update21
Run update21 and change to your w04-while directory.
[~]$ cd ~/cs21/inclass 
[inclass]$ cd w04-while/
[w04-while]$ ls
coinSim.py        randOps.py  syracuse.py
example_funcs.py  safeInt.py  whileLoop.py
[w04-while]$ 
While loops
In addition to for loops, python supports while loops which combine elements of a for loop and an if statement. Open whileLoop.py to see a basic example.
The random number module

Sometimes we want the computer to pick a random number in a given range, pick a random element from a list, pick a random card from a deck, flip a coin, etc. The random module provides access to functions that support these types of operations. The random module is another library of functions that can extend the basic features of python. The math modules is another example of extending core python functionality. For a full list of python modules, see the online documentation listing all of the default modules. To get access to the random module, we add from random import * to the top of our program (or type it into the python shell).

Open the file randOps.py in vim, and run the program in a separate terminal. Note if you run the program again, you get different (random) results. This program illustrates the functions randrange and random. We will most often be using randrange. This function generates a list just like range, but then returns one item randomly from that list.

Function basics
A function is a named block of code that acts like a small subprogram. The basic syntax for defining a new function is shown below:
def <function name>( <parameters>):
  """
  <descriptive comment>
  """
  <body>
When using a function, you should treat a function as a black box that takes input, performs some well defined task with that input, and possibly returns some output. If you understand what the function is supposed to do, you do not need to understand how the function actually performs that task. Think about some functions that you have used already. What do they do? How do they work? You may not know the answer to the second question, and you do not need to know the answer. You should keep this point in mind when writing your functions. Users should not need to know how a function works to simply use the function.

Functons have a number of uses:

A few first example
open example_funcs.py in vim. We will look over the code which contains calls to four functions, each is an example of functions that do or do not take input values and functions that do or do not return a value. Once we think we understand what the program is doing, let's try running it.

Function terminology

Tracing functions
Code containing functions is typically processed in a very non-linear way. When a function is called, the following steps take place:
  1. The calling function suspends execution at the point of the call.
  2. The values of the arguments from caller are copied to the parameters of the function in order.
  3. The body of the called function executes.
  4. The return value of the called function is returned to the point at which the function was called
  5. The calling function continues to execute the rest of its body.
Python keeps track of all of this using stack frames. You should too!