Preliminary Software Engineering
Unit 1 - Programming Fundamentals
Unit 1 - Programming Fundamentals
  • 1 - Programming Fundamentals
    • Programming Fundamentals Content
  • 2 - Python
    • Expected Python Knowledge
    • GitHub
    • Learning Python
    • Data Structures and File Management
      • Data Structures
        • Lists
        • Arrays
          • Single and Multi-Dimensional Arrays
        • Lists vs Arrays
          • Activities
        • Tuples
        • Sets
        • Dictionaries
          • Activities
      • File Handling
        • Loops, Lists, Dictionaries
        • Activities
  • 3 - Theory Content
    • Theory Content Explained
      • NESA Directional Verbs
      • Responding to Directional Verbs
  • 4 - Software Development and Management
    • Approaches to Software Development
      • Waterfall Model
      • Agile Model
      • Summary: Waterfall vs Agile
      • Activities
    • Software Development Steps
      • Sample Exam Question
      • Requirements Definition
        • Understanding the Need
        • Key Questions to Ask
        • Examples
        • Activities
        • Sample Exam Question
      • Determining Specifications
        • Functional Specifications
        • Non-Functional Specifications
        • Activities
      • Design
        • Top-Down Design Approach
        • Bottom-Up Design Approach
      • Development
        • Optimising Code
      • Integration
        • Example: Payment Program
        • Activity
        • Application Programming Interface (API)
          • Example: OpenWeather API
          • Example: WeatherAPI
          • Activity: Prepare Spells
      • Testing and Debugging
        • Test Data
          • Activities
        • Testing the System
          • Activities
        • Debugging
          • Types of Errors
            • Activities
          • Python Debugger
            • Activities
          • VS Code Debugger
            • Activities
      • Installation
        • Activities
        • Sample Exam Question
      • Maintenance
  • Charts and Algorithms
    • Example: IPO Charts and Pseudocode
      • Activities
    • Algorithms, Flowcharts, Pseudocode
      • Pseudocode Activities
      • Flowchart Activities
      • Sample Exam Questions
    • Structure Charts
      • Activities
      • Sample Exam Questions
    • Data Flow Diagrams
      • Activities
    • Data Dictionaries
      • Activities
    • Decision Trees
      • Activities
      • Sample Exam Questions
    • Gantt Chart
    • Class Diagrams
      • Sample Exam Question
    • Storyboards
      • Sample Exam Question
  • Testing and Debugging
    • Test Data
      • Activities
    • Testing the System
      • Activities
    • Debugging
      • Types of Errors
        • Activities
      • Python Debugger
        • Activities
      • VS Code Debugger
        • Activities
  • Computational Thinking
    • Decomposition
    • Abstraction
    • Activities
  • Version Control
    • Git
    • GitHub
    • Activities
  • Number Systems
    • Binary Systems
    • Hexadecimal Numbers
    • Using Two's Complement
    • Activities
  • 5 - Assessment Task 1
    • Data Science Project
      • Before we Start
        • Setting up GitHub Repository
        • Setting Up Markdown Documentation
      • Examples of API Usage
        • Starter Code: NASA Scenario
        • Starter Code: Spell Book
        • Starter: Pokédex Explorer
        • Starter Code: Weather App
        • Example: OpenWeather API
        • Example: WeatherAPI
        • Example: Prepare Spells
    • Task Guide
      • Requirements Definition
      • Determining Specifications
        • Use Cases
      • Design
        • Gantt Chart
        • Structure Chart
        • Algorithms
        • Data Dictionary
      • Development
        • Comments vs DocStrings
        • UI - main.py
        • Create Python Module
          • Example: NASA Module
          • Example: WeatherFetch Module
          • Example: SpellBook Module
      • Integration
        • Example: Pokedex
      • Testing and Debugging
        • Commit Changes
      • Installation
      • Maintenance
    • Submitting Your Task
Powered by GitBook
On this page
  • Python Debugger Commands
  • Breakpoints
  • Single Line Stepping
  • Interfaces between Functions
  1. Testing and Debugging
  2. Debugging

Python Debugger

We're going to first start by looking at the standard Python Debugger, then the VS Code debugger.

Python Debugger Commands

When using the Python Debugger, enter one of these 3 commands.

  • n (next): Executes the next line of code (steps over function calls).

  • s (step): If you were to use this instead of n, it would step into the function call.

  • c (continue): Continues the program execution without stopping.

  • p (print): Prints the value of a variable or expression. Example: p total to print the value of the total variable.

  • whatis: Prints the type of a variable. Example: whatis total to check the type of the total variable (e.g., int, float, etc.).

Breakpoints

Breakpoints are a fundamental debugging tool that allows a programmer to pause the execution of a program at a specific point in the code. This pause enables the developer to inspect the current state of the program, including the values of variables and the flow of execution. By strategically placing breakpoints in the code, a programmer can isolate and examine sections of the program where issues are suspected, making it easier to identify and fix errors.

Python:

import pdb  # Import the Python debugger

def add(a, b):
    result = a + b 
    pdb.set_trace()  # Pause the program here and start the debugger
    return result 

x = 2 
y = 3  
sum_result = add(x, y)  
print(sum_result)  

Debugger:

> <your_file>.py(6)add()
-> return result
(Pdb) p result
5
(Pdb) n
> <your_file>.py(8)<module>()
-> print(sum_result)
(Pdb) c
5

Single Line Stepping

Single line stepping is another essential debugging technique that works hand-in-hand with breakpoints. Once the program is paused at a breakpoint, single line stepping allows the developer to execute the program one line at a time. This step-by-step execution helps the programmer closely monitor the behaviour of the program, observe changes in variables, and understand the flow of control through the code. Single line stepping is particularly useful for identifying logical errors, as it provides a clear view of how each line of code affects the program's state.

Python

import pdb

def calculate_total(price, quantity):
    total = price * quantity  # Line 3
    return total  # Line 4

def apply_discount(total, discount):
    return total - (total * discount)  # Line 6

def main():
    price = 100
    quantity = 5
    discount = 0.1  # 10% discount
    total = calculate_total(price, quantity)  # Line 10
    total_after_discount = apply_discount(total, discount)  # Line 11
    print(f"Total after discount: {total_after_discount}")  # Line 12

pdb.set_trace()  # Pause the program here

main()

Debugger

> <your_file>.py(12)<module>()
-> main()
(Pdb) s
> <your_file>.py(4)main()
-> price = 100
(Pdb) s
> <your_file>.py(5)main()
-> quantity = 5
(Pdb) s
> <your_file>.py(6)main()
-> discount = 0.1
(Pdb) s
> <your_file>.py(7)main()
-> total = calculate_total(price, quantity)
(Pdb) s
> <your_file>.py(3)calculate_total()
-> total = price * quantity
(Pdb) s
> <your_file>.py(4)calculate_total()
-> return total
(Pdb) s
> <your_file>.py(8)main()
-> total_after_discount = apply_discount(total, discount)
(Pdb) s
> <your_file>.py(6)apply_discount()
-> return total - (total * discount)
(Pdb) s
> <your_file>.py(9)main()
-> print(f"Total after discount: {total_after_discount}")
(Pdb) s
Total after discount: 450.0
> <your_file>.py(10)<module>()
-> 
(Pdb) 

Watches are a feature that allows developers to keep track of specific variables or expressions as the program runs. By setting up watches, a programmer can monitor the values of these variables in real-time, even as the program continues to execute. This is invaluable when trying to understand how certain values change over time or when debugging complex conditions that depend on multiple variables. Watches provide a dynamic view of the program's internal state, making it easier to identify when and where things go wrong.

Python

import pdb

def calculate_total(price, quantity):
    total = price * quantity  # Line 3
    return total  # Line 4

def main():
    price = 50
    quantity = 3
    total = calculate_total(price, quantity)  # Line 7
    print(total)  # Line 8

pdb.set_trace()  # Pause the program here

main()

Debugger

(Pdb) s
--Call--
> <your_file_path>(3)calculate_total()
(Pdb) s
--Line 4--
> <your_file_path>(4)calculate_total()
(Pdb) s
--Return--
> <your_file_path>(4)calculate_total() 15
(Pdb) s
--Return--
> <your_file_path>(7)main()
(Pdb) s
--Line 8--
> <your_file_path>(8)main()
(Pdb) s
--Return--
> <your_file_path>(8)main()
(Pdb) s
--Return--
> <your_file_path>(8)main()
(Pdb) p total
15
(Pdb) whatis total
int

Interfaces between Functions

Functions often interact with one another by passing data or calling each other’s methods. The interface between functions refers to the way these functions communicate, including the parameters they accept, the values they return, and the side effects they produce. When debugging, it is important to ensure that data passed between functions is correct and consistent, as errors in these interfaces can lead to unexpected behaviour and bugs. Understanding these interfaces helps developers trace the flow of data through the program and identify where issues may arise.

PreviousActivitiesNextActivities

Last updated 3 months ago