Lab 04

Objectives:


Introduction

A CHANGE

This week I will ask you to work with a partner of your choice.  You will turn in a SINGLE answer sheet for both partners. 

I will talk about this at the start of the lab today, but to put it in writing  - when you work with a partner you should:

Failure to follow these guidelines may cause you to lose points for this activity.


Activity  A : An introduction to iteration - while

(Partner A types, Partner B writes)

There are two forms of iteration used in Python - the "while" structure and the "for" structure.  Both are effective ways to loop, but each is most appropriate in different situations.  In this lab today, you will look at several examples of how each structure works.  Let's begin by looking at a while structure.

A while structure is a structure that repeats a suite of code as long as a Boolean condition is True.  It is called a sentinel-controlled loop because the Boolean condition is a "sentinal" value that helps decide whether or not the suite of code should be repeated.

Generically it looks like this:

Consider the following code that uses a while loop to calculate a student's average quiz score.  From the python shell window open a new programming window by selecting "File | New Window" In this window type in the Python program :

Run this program and observe how it works.

[Q1]  What is the purpose of the variable named "numberOfQuizzes"

[Q2]  What is the purpose of the variable named "total"

[Q3]  What is the purpose of the variable named "quizCount"
 


Activity  B : Obnoxious kid in my car

(Partner A types at the computer)

Ann Oying contacts you about writing a simulator that demonstrates what is like to be in a car with a child on a long road trip. 

For example, 

(The string "Are we there yet?" is printed once for each year of the child's age.) You should write this code based on a while loop.

[SIG1]  Show this to an instructor when you have this completed


Activity  C : An introduction to iteration - for

(Partner B types at the computer, Partner A writes)

The for loop is another looping structure.  It is called a count-controlled loop because it is used in situations where an iterable data structure is involved and it's size (or "count") is known in advance.  Generically speaking, it looks like the following:

 

To demonstrate a for loop we have to first create an iterable data structure.  The quickest and easiest to get started is the list structure produced by the range function. Remember that a list structure is a data strcture that holds one or more values. The range function produces a range of values. Combining them, we can use the range function to place a range of values in a list.

[Q4]  For each of the following uses of range, first PREDICT what will be returned when you type this into the python shell.  Then, test the command out and see if you were correct.

Command Prediction Actual result
list(range(5))    
list(range(8))    
list(range(0,5))    
list(range(3,9))    
list(range(9,3))    
list(range(2,10,1))    
list(range(2,10,2))    
list(range(2,10,3))    
list(range(10,2,2))    
list(range(10,2,-2))    

 

Let's consider a couple of summary questions

[Q5]  Consider the list produced by the one parameter version  -- range(a). 

The first number in the list is:

The last number in the list is:

The distance between each number in the list is:

 

[Q6]  Consider the list produced by the two parameter version -- range(b,c) 

The first number in the list is:

The last number in the list is:

The distance between each number in the list is:

 

[Q7]  Consider the list produced by the three parameter version -- range(x,y,z) 

The first number in the list is:

The last number in the list is:

The distance between each number in the list is:

 

 

[Q8]  What do you notice about the last value actually produced by the list and the "ending value" in the range function (For this question you may assume that the distance value is 1).

 

[Q9]  What do you notice about the number of values produced in a range function? How does it relate to the "starting value" and the "ending value"? (For this question, you may assume that the distance value is 1)?

 

 

 


Activity  D : Using the for loop with Average Quiz Score

(Partner A types at the computer)

From the python shell window open a new programming window by selecting "File | New Window"

In this window type in the Python program below which calculates the average score from several scores

Save this as "quiz.py" and run it.

 

 

Test this program by using the 3 quiz scores of 10, 20, and 30 (which should have an average of 20).

 

 

Notice that this program does not work the way it should.  We have made a logical error (as opposed to a syntax error).  Fix this problem.

[SIG2]  Show this to an instructor when you have this completed


Activity  E : Obnoxious kid in my car, for loop

(Partner B types at the computer)

Rewrite your solution to Activity B so that it uses a for loop instead of a while loop

[SIG3]  Show this to an instructor when you have this completed


Activity  F: More with the while structure

(Partner A types, Partner B writes)

So far we have looked at both the while structure and the for structure and it doesn't look like they are that different.  Both were used to solve the same basic problem.  If they are that similar, why have both structures?

We have two different structures because not all situations where we are looping have known finish times or "counts" when we begin.  Consider a situation where we are entering quiz scores.  Not all situations involve knowing the number of quizzes when we get started For example, suppose you are reading scores from a file.  You don't know how many there are until you get to the end of the file.

In this situation, we want to use a while loop and define a "sentinal value" that signals that we are done taking in data. 

For example, type in the following program:

 

At that point the computer should calculate and print the average.  Something like:

 

[Q10]   What is the purpose of the variable named "total"

[Q11]   What is the purpose of the variable named "count"

[Q12]   What is the purpose of the variable named "oneScore"

[Q13]  Why do we ask the "Enter a score" question twice?


Activity  G : Obnoxious Kid

(Partner B types at the computer)

You may recall from our earlier conversations that the computer uses 0 for False and 1 (among other values) for True.  Let's think about the obnoxious kid problem as a sentinel value problem.  Revise your code to keep asking "Are we there yet"  But this time, actually wait for an answer.  If the human types in 0 then they are saying "False.  We aren't there."  When you finally get there the human will type in "1" or True.

Your running code should look like this:

[SIG4]  Show this to an instructor when you have this completed