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
  1. 5 - Assessment Task 1
  2. Task Guide
  3. Development

Create Python Module

Now we need to create our module where we will import all of our functions from.

Essentially, you are just creating your functions in a separate .py file, so we do not cluttermain.py.

It must be in the same folder.

We will integrate them LATER - in the INTEGRATION step!

The following is an example of the pokedex.py module.

pokedex.py
import requests # Must import requests so we can use API

# API Base URL for PokéAPI
API_URL = "https://pokeapi.co/api/v2/pokemon/"

# Dictionary to store collected Pokémon
pokedex = {}

def search_pokemon(name):
    """Search for a Pokémon by name or ID and return its details.""" # Triple quotes is a docstring - allows multiline comments!
    response = requests.get(f"{API_URL}{name.lower()}")
    if response.status_code == 200:
        data = response.json()
        return {
            "name": data["name"].capitalize(),
            "id": data["id"],
            "height": data["height"],
            "weight": data["weight"],
            "types": [t["type"]["name"] for t in data["types"]]
        }
    else:
        print("Pokémon not found.")
        return None

def add_pokemon(name):
    """Add a Pokémon to the Pokédex."""
    pokemon = search_pokemon(name)
    if pokemon:
        pokedex[pokemon["name"]] = pokemon
        print(f"{pokemon['name']} added to Pokédex.")

def view_pokedex():
    """Display all collected Pokémon."""
    if pokedex:
        for name, details in pokedex.items():
            print(f"{name} - ID: {details['id']}, Height: {details['height']}, Weight: {details['weight']}, Types: {', '.join(details['types'])}")
    else:
        print("Your Pokédex is empty.")

def remove_pokemon(name):
    """Remove a Pokémon from the Pokédex."""
    if name.capitalize() in pokedex:
        del pokedex[name.capitalize()]
        print(f"{name.capitalize()} removed from Pokédex.")
    else:
        print("Pokémon not found in your Pokédex.")

#<------------------------------TEST YOUR FUNCTIONS BELOW------------------------------>

Now, with the above we have defined all functions and included the API call. Keep in mind some APIs such as WeatherAPI will require a key as well - a separate example will be provided.

However, we now need to test each function. Rather than having to test each in the UI and spend forever, it's often best to just run the functions below our code here.

Here is an example of how you might test each function:

#<------------------------------TEST YOUR FUNCTIONS BELOW------------------------------>
print(search_pokemon('Pikachu'))

As the search_pokemon() function only returns a value and does not print if it is successful, we need to add the print statement to the function call to see what values it actually returns


#<------------------------------TEST YOUR FUNCTIONS BELOW------------------------------>
add_pokemon('Pikachu')

This function actually has print statements, so we can test it just as above


#<------------------------------TEST YOUR FUNCTIONS BELOW------------------------------>
add_pokemon('Pikachu')
view_pokedex()
remove_pokemon('Pikachu')

Some functions may depend on other functions being run first. For instance, we cannto view a pokemon in our pokedex or remove it unless we add one first.


#<------------------------------TEST YOUR FUNCTIONS BELOW------------------------------>
view_pokedex()
remove_pokemon('Pikachu')

In saying that, we still need to test both on their own so we know that all possible outcomes will work.

PreviousUI - main.pyNextExample: NASA Module

Last updated 3 months ago