Description
OBJECTIVE
The objective of this assignment is to give you practice with nested loops.
ASSIGNMENT SUBMISSION
While the lab instructor walks around checking lecture exercises (pre-lab) on individual basis, start working with the assigned partner on exercise sets 2 and 3. Once you are done with a set, check with the one of the other pairs sitting next to you regarding their progress – help each other. Then, as a pair, present your solutions to the lab instructor. Each student is to have his version of the programs/answers and be capable of presenting them for the pair. The presenter will be chosen at random by the lab instructor. All the exercises other than the last set need to be shown to the lab instructor before leaving the lab for full credit. The last exercise set may be finished at home and shown in the beginning of the subsequent lab session. Use IDLE unless indicated otherwise.
1. Lecture Exercises (20%)
Show the following exercises you were to create during the lecture:
• ascending.py
• whileExample.py (containing nested loops: outer one asking if a user wants to repeat the program; two inner sequential loops dealing with a negative radius and an invalid choice)
2. Adding outer loops (25%)
a. Find trasnsposition.py and add an outer loop that allows a user to repeat the program.
b. Find gangsta.py and add an outer loop that will repeat the program 3 times.
3. Nested loops (30%)
For each program given below, perform paper and paper trace of all the program’s variables and show the output generated by the program. In order to perform a trace, build a table similar to the one you used for lab 4. After your trace is done, run the program to confirm your outcomes and learn from any misunderstandings you may have (the first program contains print statements that will help you visualize the code; you may add similar print statements to programs in parts b and c)
a.
i = 3
while i >= 1:
j = 1
while j < 4:
j += 1
i -= 1
b.
counter = 0
total = 0
for i in range(1, 3, 1):
total += i
for j in range(1, 5, 1):
total += j
counter += 1
c.
x = 3
y = 18
finished = False
while x <= y and not finished:
subtotal = 0
for z in range(x):
subtotal += x
if y // x <= 2:
finished = True
else:
x += 2
4. Problem solving with nested loops (25%)
a. Write nested for loops to produce the following output:
1111111
2222222
3333333
4444444
5555555
6666666
7777777
1
22
333
4444
55555
666666
7777777
b. Write a program that uses nested loops to collect data and calculate the average rainfall over a period of weeks. The program should first ask for the number of weeks. The outer loop will iterate once for each week. The inner loop will iterate 7 times, once for each day. Each iteration of the inner loop will ask the user for the inches of rainfall for that day. After all iterations, the program should display the number of weeks, the total inches of rainfall, and the average rainfall per week for the entire period. These are sample program runs:
Enter the number of weeks: 0
Number of weeks: 0
Total rainfall: 0
Average rainfall: 0
———————-
Enter the number of weeks: 2
Enter the rainfall for day 0: 1.2
Enter the rainfall for day 1: 1.6
Enter the rainfall for day 2: 3
Enter the rainfall for day 3: 3
Enter the rainfall for day 4: 3
Enter the rainfall for day 5: 1
Enter the rainfall for day 6: 1
Enter the rainfall for day 0: 1
Enter the rainfall for day 1: 1
Enter the rainfall for day 2: 1
Enter the rainfall for day 3: 1
Enter the rainfall for day 4: 1
Enter the rainfall for day 5: 1
Enter the rainfall for day 6: 1
Number of weeks: 2
Total rainfall: 20.8
Average rainfall: 10.4
—– THE END —–
Lab Assignment 5 TCSS 142



