Lab Assignment 5, TCSS 142 solution

$25.00

Category:

Description

Rate this product

Lecture Exercises (20%)

Show the following exercises you were to create during the lecture:

  • py
  • 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)

 

  1. Adding outer loops (25%)
  2. Find trasnsposition.py and add an outer loop that allows a user to repeat the program.
  3. Find gangsta.py and add an outer loop that will repeat the program 3 times.

 

  1. 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

 

 

  1. Problem solving with nested loops (25%)

 

  1. Write nested for loops to produce the following output:

 

1111111

2222222

3333333

4444444

5555555

6666666

7777777

 

 

1

22

333

4444

55555

666666

7777777

 

  1. 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 —–