Lesson Notes By Weeks and Term v5 - Grade 12

Programming for problem solving and projects – Week 10 focus

Download the Lessonotes Mobile South Africa app for faster lesson access on Android and iPhone.

Subject: Information Technology

Class: Grade 12

Term: 1st Term

Week: 10

Theme: General lesson support

Lesson Video

This page supports the lesson note with a companion video and a short classroom-ready summary.

For class groups and homework, share this lesson page so learners also get the summary, objectives, and full lesson context.

Performance objectives

Lesson summary

This week focuses on solidifying your problem-solving skills through programming, specifically in the context of project development. We will concentrate on refining our algorithms, implementing robust error handling, and enhancing the user experience. In South Africa, effective software development skills are crucial for creating innovative solutions to local challenges – from improving access to education and healthcare through mobile apps to optimising agricultural processes using data analytics. Strong programming skills open doors to careers in software development, data science, cybersecurity, and many other rapidly growing fields.

Lesson notes

Algorithm Design & Data Structures: An algorithm is a step-by-step procedure for solving a problem. Choosing the right data structure is crucial for efficient algorithm implementation.

Lists: Ordered collections of items. Useful for storing and accessing sequences of data.

Example: `[1, 2, 3, 4, 5]` Dictionaries: Key-value pairs. Useful for storing and retrieving data based on unique identifiers.

Example: `{"name": "Thabo", "age": 20, "city": "Johannesburg"}` Sets: Unordered collections of unique items. Useful for removing duplicates and performing set operations (union, intersection, difference).

Example: `{1, 2, 3, 4, 5}` Worked Example (South African context): Problem: You are tasked with developing a system to manage student grades for a class. The system should allow you to add student names and their corresponding marks for a specific subject.

Algorithm: Use a dictionary to store student names as keys and their marks as values.

Create functions to: Add a student and their mark to the dictionary. Retrieve a student's mark. Calculate the average mark for the class. Implement error handling to ensure that the entered marks are valid (e.g., between 0 and 100).

Python implementation (Illustrative): ```python def calculate_average_mark(student_grades): """Calculates the average mark for the class.""" total_marks = sum(student_grades.values()) number_of_students = len(student_grades) if number_of_students > 0: return total_marks / number_of_students else: return 0 # Return 0 if no students are in the dictionary student_grades = {"Sipho": 75, "Aisha": 82, "Thando": 90} average_mark = calculate_average_mark(student_grades) print(f"The average mark is: {average_mark}") # Output: The average mark is: 82.33333333333333 ``` Error Handling (Try-Except): Error handling is essential for creating robust applications. The `try-except` block allows you to gracefully handle potential errors during program execution, preventing crashes and providing informative messages to the user. Worked

Example: Problem: You are developing a program that divides two numbers entered by the user. Handle the `ZeroDivisionError` if the user enters 0 as the divisor. ```python try: numerator = int(input("Enter the numerator: ")) denominator = int(input("Enter the denominator: ")) result = numerator / denominator print(f"The result is: {result}") except ZeroDivisionError: print("Error: Cannot divide by zero.") except ValueError: print("Error: Invalid input. Please enter numeric values.") ``` Explanation: The `try` block attempts to execute the code within it. If a `ZeroDivisionError` occurs (division by zero), the `except ZeroDivisionError` block is executed, printing an error message. The second `except` block will catch any `ValueError` that comes from non-numeric input to `int()`.

Code Refactoring: Code refactoring involves improving the internal structure of existing code without changing its external behaviour.

This includes: Improving Readability: Using meaningful variable names, adding comments, and structuring code logically.

Reducing Redundancy: Identifying and eliminating duplicate code blocks by creating reusable functions.

Improving Efficiency: Optimising algorithms and data structures to reduce execution time and memory usage. Worked

Example: Original Code (Less Readable): ```python a = [1,2,3,4,5] b = 0 for c in a: b = b + c print(b) ``` Refactored Code (More Readable): ```python numbers = [1, 2, 3, 4, 5] total = 0 for number in numbers: total += number print(total) ``` Explanation: The refactored code uses more descriptive variable names (`numbers` instead of `a`, `total` instead of `b`, `number` instead of `c`). This makes the code easier to understand and maintain. Also, `b = b + c` was shortened to `total += number` for brevity and clarity.

User Interface (UI)

Design: A good UI is essential for user satisfaction.

Consider factors like: Simplicity: Keep the interface clean and uncluttered.

Intuitive Navigation: Make it easy for users to find what they need.

Clear Feedback: Provide informative messages to the user about their actions. This section will cover basics. More advance UI libraries are beyond the scope of this week. The primary method for UI this week is a CL

I. Version Control (Git): Version control systems like Git are invaluable for collaborative software development. Git allows you to track changes to your code, revert to previous versions, and collaborate with others without overwriting each other's work.

Key concepts include: Repositories: A directory containing all the files and the history of changes for a project.

Commits: Snapshots of the project at a specific point in time.

Branches: Independent lines of development.

Merging: Combining changes from different branches. Guided Practice (With Solutions)

Question 1: Write a Python function that takes a list of South African province names as input and returns a dictionary where the keys are the province names and the values are their corresponding capitals.