Computer Science Principles
Course Progress
0/0
Objectives in LxD
3.4 Strings
3.4 Strings
Crowdsourcing Lesson
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
ML | Fitness
ML | Neural Network | Handwritting Detection
Data Frames | Pandas | Intro 2
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
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
2 min read
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 |
# 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]))
# 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)
# 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}")
# 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!)
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
# 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]))
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.
# 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)}")
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 |