CSC 1001 — Lab 3: Tracing, Types, and Testing
Intro
This lab has you trace and test code individually, then compare your results and code with a partner.
| Section | Mode | What You'll Do |
|---|---|---|
| Understand | Individual | Trace three code chunks by hand and predict errors, output, and variable types. |
| Do | Individual | Test your traced code, add explicit typing, and solve the Temperature Advisory problem. |
| Communicate | Partners | Compare your Understand and Do results with a partner. |
Submit: your Lab 3 Worksheet (traces and answers to the questions, including reflections) and your Lab3.ipynb notebook (all non-trace work from the Do section).
Understand (Individual)
Setup: For each code chunk below, trace through it by hand before running anything.
For each chunk, answer:
- Will the chunk cause an error or not?
- If no error, what will get printed?
- If an error, what is the cause? How would you fix it?
- For every variable in the chunk, what type is it (int, bool, str, float), and what is its purpose in the code?
Chunk 1
age = input("Enter your age: ")
next_year = age + 1
print("Next year you will be " + next_year)Chunk 2
total_items = 7
boxes = 2
items_per_box = total_items // boxes
print("Each box will have " + str(items_per_box) + " items, with none left over.")Chunk 3
name = "Jordan"
score = 87
bonus = 5.5
final_score = score + bonus
message = name + " earned a final score of " + str(final_score) + " points."
print(message)Reflection Questions
Record your answers on your Lab 3 Worksheet before moving to the Do section.
Do (Individual)
Write the code for all three steps in the same notebook. Use a different cell for each chunk of code and a different cell for the coding section of step 3. You should have at least 4 cells in your notebook by the time you submit it.
Step 1: Test the code from Understand
Take the code segments above and actually run them. If any have errors, fix the errors before running.
- Try several different inputs for each chunk. (This may mean changing the code.) How are you choosing what to try? (Think about typical values, edge cases, and anything that seems likely to break it.)
- Record the inputs you try in the table on your Lab 3 Worksheet, noting whether each works as expected and, if not, why it fails.
Step 2: Add explicit typing
Go back through the code segments in your Jupyter Notebook and update the code so every variable's type is made explicit.
Step 3: Solve a new problem
Problem: Temperature Advisory
A weather app asks the user for the current temperature in Fahrenheit. Since anything typed in by the user comes in as text, the program needs to:
- Convert the entered temperature into a number
- Convert that Fahrenheit temperature into Celsius (
C = (F - 32) * 5 / 9) - Decide on a category:
- Below 50°F is "cold"
- 50°F up to (but not including) 80°F is "mild"
- 80°F and above is "hot"
- Print both temperatures and the category
Work through the full process:
- List requirements — What does the program take in? What must it produce? What rules apply?
- Break down the problem — What are the smaller steps or decisions involved?
- Write the pseudocode — Plain-English steps, no need for formal syntax.
- Write the code — Now translate your pseudocode into actual Python. (You don't need to print the category for this section.)
Reflection Questions
Record your answers on your Lab 3 Worksheet before moving to Communicate.
Communicate (Partners)
Setup: Find someone else who has finished Do. Compare your results from Understand and Do, then record your discussion and reflection answers on your Lab 3 Worksheet.