CSC 1001 — Lab 6: Loops — Trace, Build, and Choose

Intro

Today's lab is about loops — how they repeat work, how to follow their execution one step at a time, and how to decide whether a for loop or a while loop is the better fit for a problem. You will not be graded on writing perfect Python syntax. You will be graded on whether you can trace what code does, spot where it breaks, and reason clearly about the steps a program needs to take.

SectionModeWhat You'll Do
UnderstandIndividualTrace three short loop code snippets and predict their output.
DoIndividual & PartnerDesign and build solutions to the Ticket Counter problem (individual) and the Password Attempts problem (partner).
CommunicatePartnerDiscuss which loop type fits the Password Attempts problem, and why.

Submit: your Lab 6 Worksheet and your Lab6.ipynb notebook (Problem Z code).


Understand (Individual)

Work alone for this section. For each trace, follow the code one line at a time and record what each variable holds on your Lab 6 Worksheet, along with the final printed output.

Code Trace 1 — Summing a List (for loop)

1 numbers: list[int] = [4, 8, 15, 16, 23, 42]
2 total: int = 0
3 for n in numbers:
4     total = total + n
5 print(total)

Code Trace 2 — Counting Vowels (while loop)

1 word: str = "banana"
2 index: int = 0
3 count = 0
4 while index < len(word):
5     if word[index] in "aeiou":
6         count = count + 1
7     index = index + 1
8 print(count)

Code Trace 3 — Find the Bug (for loop)

This one has a bug. Trace it exactly as written — don't fix it yet.

1 scores: list[int] = [85, 90, 78, 92]
2 total: int = 0
3 count: int = 0
4 for i in range(1, len(scores)):
5     total = total + scores[i]
6     count = count + 1
7 average: float = total / count
8 print(average)

On your worksheet, record what this code actually prints, what it should print if it worked correctly, and where the bug is, then answer the other questions before the Do section.


Do

Now you'll move from reading loops to designing them. For each problem, work through the full pipeline: requirements → subtasks → pseudocode → code. You are not writing Python from a blank page — you are reasoning about the steps and, in Problem Y, arranging code that's already written for you.

Ticket Counter Problem (Individual)

A movie theater wants a program that looks at a list of customer ages and reports how many child tickets (age 12 or under) and how many adult tickets (age 13 or older) it needs to sell. For example, given the list [8, 15, 12, 4, 30, 13], the program should report 3 child tickets and 3 adult tickets.

Step 1 — Requirements List what the program needs as input, what it needs to produce as output, and any rules it must follow. Record this on your Lab 6 Worksheet.

Step 2 — Subtasks Break the problem into the smaller steps the program must perform, in order. Record this on your worksheet.

Step 3 — Pseudocode Write the logic in plain, structured language — no Python syntax required. Record this on your worksheet.

Step 4 — Code from Blocks The lines of a working solution are shuffled below. On your worksheet, write the letters in the correct order to build a program that solves the problem, then explain how you decided on the order.

Afor age in ages:
Badult_count = adult_count + 1
Cprint(child_count, adult_count)
Dchild_count: int = 0
Eadult_count: int = 0
Fif age <= 12:
Gchild_count = child_count + 1
Helse:
Iages: list[int] = [8, 15, 12, 4, 30, 13]

Password Attempts Problem (Partners)

A login system should ask the user to enter a password. If the password is correct, it should print "Access granted" and stop asking. If it's wrong, it should let the user try again — but only up to 3 total attempts. If the user runs out of attempts without success, the program should print "Account locked."

Step 1 — Requirements (as a pair) Record this on your worksheet.

Step 2 — Subtasks (as a pair) Record this on your worksheet.

Step 3 — Pseudocode, written twice, from scratch Split up: one partner designs the solution using a for loop, the other using a while loop. Neither of you may look at Python syntax — write structured plain-language steps on your worksheet.

Step 4 — Code from Scratch (together) Combine your two pseudocode versions, agree on which approach to build, and write the actual Python code together in Lab6.ipynb.


Communicate (Partner)

Discuss the following with your partner, then write a short answer for each. Be ready to share your reasoning with the instructor and other students.

1. For the Password Attempts problem, which loop type makes more sense — for or while? Why?

2. Is one type of loop always going to make more sense for a given problem, or can the same problem be solved well with either? Why or why not?

Record your answers on your Lab 6 Worksheet.