Question: for Loop lab Objectives: Explore techniques for repetition (for loops) Use the range function to create a sequence. Record you answers to the questions on
for Loop lab
Objectives:
Explore techniques for repetition (for loops)
Use the range function to create a sequence.
Record you answers to the questions on this sheet.
Format your answers so they are distinguished.
Activity D: An introduction to iteration - for
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. A list structure is a data structure 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.
Question 1 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. Record answers on This Sheet.
Question 2 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:
Question 3 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:
Question 4 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:
Question 5 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).
Question 6 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 E: An introduction to iteration - for
From the Python shell window open a new program window by selecting File | New File.
The Python program below calculates the average score from several scores.
This program is broken.
This program is "quiz.py".
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). Write a version of this program with a fix for this problem.
***** Submit the file with the fix. Name the file quiz.py*******
Activity F: Obnoxious kid in my car, for loop
Rewrite your solution to Activity B so that it uses a for loop instead of a while loop.
Activity B : Obnoxious kid in my car
Ann Oying contacts you about writing a simulator that demonstrates what it is like to be in a car with a child on a long road trip.
For Example,
Enter the childs age: 5
Are we there yet?
Are we there yet?
Are we there yet?
Are we there yet?
Are we there yet?
The string Are we there yet? is printed once for each year of the childs age. You should write this code in a new file based on a for loop. Name the file annoying.py. Before writing the Are we there yet? add a comment to your program file.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
