Computer Science Principles
Course Progress
0/0
Objectives in LxD
3.4 Strings
3.4 Strings
3.3 Mathematical Expressions
3.3 Math Expressions
3.2 Data Abstractions
3.2 Data Abstractions
3.2 Data Abstractions
3.1 Variables & Assignments
3.1 Variables and Assignments
3.1 Variables and Assignments (Sample)
Intro to Python
Intro to Javascript
3.5 Boolean Expressions (PY)
3.5 Boolean Expressions (JS)
3.8 Iterations
3.7 Nested Conditionals
3.6 Conditionals
3.8 Iterations
3.7 Nested Conditionals
3.6 Conditionals
3.13 Developing Procedures
3.12 Calling Procedures
3.10 Lists
3.13 Developing Procedures
3.10 Lists
3.9 Developing Algorithms
3.17 Algorithmic Efficiency
3.9 Algorithms
3.17 Algorithmic Efficiency
3.15 Random Numbers (pseudocode)
3.15 Random Numbers (js)
3.15 Random Numbers (py)
BI 3 Review
Data Frames | Pandas | Intro 1
ML | Titanic Data
Titanic Survival Predictor
Titanic Survival Predictor - Aaryav
ML | Fitness
ML | Neural Network | Handwritting Detection
Network Stack | HTTP and TCP/IP
API | Request | Response | Database
Data | SQL Connect
Data | SQLAlchemy
Data | Binary Logic
Computer System | Web Server | Flask
Topic 1.4 - Identifying and Correcting Errors
Single Responsibility & API Chaining
Topic 1.4 - Identifying and Correcting Errors
Computing Systems | AWS Deployment | Setup Applicationa
Computing Systems | AWS Deployment | Launch EC2
Computing System | AWS Deployment| Step-by-Step Guide
Topic 1.4 - Identifying and Correcting Errors
5 min read
- Topic 1.4: Identifying and Correcting Errors
- 1. Types of Errors
- Code Runner Challenge
- Code Runner Challenge
- Code Runner Challenge
- Code Runner Challenge
- 2. Debugging Strategies (CRD-2.I.5)
- Code Runner Challenge
- 3. Testing for Correctness (CRD-2.J)
- Code Runner Challenge
- AP Exam Review
- 1. Types of Errors
Topic 1.4: Identifying and Correcting Errors
Covers CRD-2.I (error types and debugging) and CRD-2.J (testing for correctness).
1. Types of Errors
| Error Type | Key Idea |
|---|---|
| Syntax Error (CRD-2.I.2) | Language rules broken — program won’t run at all |
| Logic Error (CRD-2.I.1) | Program runs but gives wrong results |
| Run-Time Error (CRD-2.I.3) | Program crashes during execution |
| Overflow Error (CRD-2.I.4) | Number outside the defined range of values |
Code Runner Challenge
Fix the syntax errors in the commented function so it runs and prints the average.
View IPYNB Source
# CODE_RUNNER: Fix the syntax errors in the commented function so it runs and prints the average.
# SYNTAX ERROR — missing colons, program won't run
# def calculate_average(scores)
# for score in scores
# total += score
# CORRECTED:
def calculate_average(scores):
total = 0
for score in scores:
total += score
return total / len(scores)
print("Average:", calculate_average([90, 85, 78, 92, 88]))
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
Code Runner Challenge
Find the logic error — the broken find_max returns 0 for all-negative input. Fix it.
View IPYNB Source
# CODE_RUNNER: Find the logic error — the broken find_max returns 0 for all-negative input. Fix it.
# LOGIC ERROR — runs but gives wrong answer
def find_max(numbers):
max_val = 0 # Bug: assumes positive numbers
for num in numbers:
if num > max_val:
max_val = num
return max_val
print(find_max([-3, -7, -2])) # Returns 0 — WRONG! Should be -2
# Fix: initialize to first element
def find_max(numbers):
max_val = numbers[0]
for num in numbers:
if num > max_val:
max_val = num
return max_val
print(find_max([-3, -7, -2])) # -2 (correct)
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
Code Runner Challenge
Run the cell — observe how try/except handles run-time errors.
View IPYNB Source
# CODE_RUNNER: Run the cell — observe how try/except handles run-time errors.
# RUN-TIME ERROR — crashes mid-execution
try:
result = 10 / 0
except ZeroDivisionError as e:
print(f"Run-time error: {e}")
try:
print([10, 20, 30][5])
except IndexError as e:
print(f"Run-time error: {e}")
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
Code Runner Challenge
Run to see an 8-bit signed integer overflow — 127 + 1 wraps around to -128.
View IPYNB Source
# CODE_RUNNER: Run to see an 8-bit signed integer overflow — 127 + 1 wraps around to -128.
# OVERFLOW ERROR — number exceeds defined range
import numpy as np
int8_max = np.int8(127)
overflowed = np.int8(int8_max + np.int8(1))
print(f"127 + 1 with 8-bit integer: {overflowed}") # -128 (wraps around!)
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
2. Debugging Strategies (CRD-2.I.5)
- Test cases — check expected outputs with different inputs
- Hand tracing — step through code on paper, tracking variables
- Visualizations — tools that show program state graphically
- Debuggers — IDE tools to step through code line by line
- Extra output statements — print variables to find where things go wrong
Code Runner Challenge
Hand-trace count_evens — the broken version starts count at 1. Fix it.
View IPYNB Source
# CODE_RUNNER: Hand-trace count_evens — the broken version starts count at 1. Fix it.
# HAND TRACING example: count even numbers
def count_evens(numbers):
count = 1 # Bug: should be 0
for num in numbers:
if num % 2 == 0:
count += 1
return count
# Hand trace [3, 4, 7, 8, 2]: count starts at 1, ends at 4 — should be 3
print("Broken:", count_evens([3, 4, 7, 8, 2]))
# Fix: start count at 0
def count_evens(numbers):
count = 0
for num in numbers:
if num % 2 == 0:
count += 1
return count
print("Fixed:", count_evens([3, 4, 7, 8, 2]))
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
3. Testing for Correctness (CRD-2.J)
- CRD-2.J.1 — Use defined inputs to check expected outcomes; revise based on results.
- CRD-2.J.2 — Test at extremes (min/max) and just beyond boundaries.
- CRD-2.J.3 — Program requirements determine what inputs to test.
Code Runner Challenge
Run the boundary tests for get_grade. Add an extra-credit case for score = 91.
View IPYNB Source
# CODE_RUNNER: Run the boundary tests for get_grade. Add an extra-credit case for score = 91.
# Boundary testing: letter grades
def get_grade(score):
if score >= 90: return "A"
elif score >= 80: return "B"
elif score >= 70: return "C"
elif score >= 60: return "D"
else: return "F"
for score, expected in [(100,"A"),(90,"A"),(89,"B"),(80,"B"),(70,"C"),(60,"D"),(59,"F"),(0,"F")]:
result = "PASS" if get_grade(score) == expected else "FAIL"
print(f" {result} | {score} -> {get_grade(score)}")
Lines: 1
Characters: 0
Output
Click "Run" in code control panel to see output ...
AP Exam Review
| Essential Knowledge | Remember |
|---|---|
| CRD-2.I.1 | Logic errors = wrong results |
| CRD-2.I.2 | Syntax errors = won’t run |
| CRD-2.I.3 | Run-time errors = crashes |
| CRD-2.I.4 | Overflow errors = out of range |
| CRD-2.I.5 | 5 strategies: test cases, hand tracing, visualizations, debuggers, print statements |
| CRD-2.J | Test with defined inputs at boundaries; need program requirements |