0% found this document useful (0 votes)
4 views

Assignment Unit 4 Submit

The document outlines a programming assignment consisting of two parts: developing a function to calculate the hypotenuse of a right triangle using the Pythagorean theorem and creating a personal finance tracker. Both projects follow an incremental development approach, ensuring functionality and correctness at each stage. The assignment emphasizes the importance of structured development for effective debugging and organization.

Uploaded by

Guru Guru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Assignment Unit 4 Submit

The document outlines a programming assignment consisting of two parts: developing a function to calculate the hypotenuse of a right triangle using the Pythagorean theorem and creating a personal finance tracker. Both projects follow an incremental development approach, ensuring functionality and correctness at each stage. The assignment emphasizes the importance of structured development for effective debugging and organization.

Uploaded by

Guru Guru
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Programming Assignment Unit 4

Part 1

As a software developer, I have been tasked with developing a function that calculates the
hypotenuse of a right triangle using the Pythagorean theorem. I will implement this using an
incremental development approach, where I build the function step by step while ensuring
correctness at each stage.

Stage 1 - Initial Function


At this stage, I define a function `hypotenuse(a, b)` that takes two arguments but does not
perform any calculations yet. Instead, it simply returns `None` as a placeholder.

Stage 2 - Input Validation


To ensure that the function only accepts valid inputs, I add a validation step. The function will
raise an error if the input values are negative, preventing invalid calculations.

Stage 3 - Implementing the Pythagorean Theorem


Now, I incorporate the actual computation using the formula:

I use Python’s `math.sqrt()` function to calculate the hypotenuse.

Stage 4 - Testing the Function


I test the function with different sets of inputs to verify its correctness.

Output
```
5.0
13.0
17.0
```

---

Part 2

Custom Function for Personal Finance Tracker

For my portfolio, I will create a **personal finance tracker** that helps users manage income and
expenses. The development will follow an **incremental approach**, ensuring that each stage is
functional and thoroughly tested.

Stage 1 - Initial Function


I begin by initializing a dictionary to store income and expense data.

```python
def initialize_tracker():
return {'income': [], 'expenses': []}
```

Stage 2 - Adding Income and Expenses


Next, I implement functions to record income and expenses.

```python
def add_income(tracker, amount, description):
tracker['income'].append({'amount': amount, 'description': description})

def add_expense(tracker, amount, description):


tracker['expenses'].append({'amount': amount, 'description': description})
```

Stage 3 - Calculating the Balance


Now, I add a function to compute the balance by subtracting total expenses from total income.

```python
def calculate_balance(tracker):
total_income = sum(item['amount'] for item in tracker['income'])
total_expenses = sum(item['amount'] for item in tracker['expenses'])
return total_income - total_expenses
```

Stage 4 - Categorizing Expenses


To enhance the functionality, I allow users to categorize expenses for better tracking.
```python
def add_expense_with_category(tracker, amount, description, category):
tracker['expenses'].append({'amount': amount, 'description': description, 'category': category})

def get_expenses_by_category(tracker):
expenses_by_category = {}
for expense in tracker['expenses']:
category = expense.get('category', 'Uncategorized')
expenses_by_category.setdefault(category, []).append(expense)
return expenses_by_category
```

Stage 5 - Testing the Function


I test the finance tracker by adding various transactions and displaying the results.

```python
# Initializing tracker
tracker = initialize_tracker()

# Adding income and expenses


add_income(tracker, 5000, "Salary")
add_income(tracker, 200, "Bonus")
add_expense(tracker, 1200, "Rent")
add_expense_with_category(tracker, 150, "Groceries", "Food")
add_expense_with_category(tracker, 50, "Internet", "Utilities")

# Displaying results
print("********** Personal Finance Tracker **********")
print("Current Balance: ${:.2f}".format(calculate_balance(tracker)))

print("\nIncome:")
for entry in tracker['income']:
print(f"- {entry['description']}: ${entry['amount']:.2f}")

print("\nExpenses by Category:")
expenses_by_category = get_expenses_by_category(tracker)
for category, expenses in expenses_by_category.items():
print(f"- Category: {category}")
for expense in expenses:
print(f" - {expense['description']}: ${expense['amount']:.2f}")
```

Output
Conclusion
This assignment demonstrates the incremental development approach by breaking the
problem into smaller, manageable parts, ensuring correctness at each stage.
- In Part 1 I developed and tested a function to calculate the hypotenuse of a right triangle.
- In Part 2 I created a personal finance tracker that allows users to record transactions,
categorize expenses, and calculate their balance.

This method ensures faster debugging, better organization, and clear documentation—essential
skills for any professional developer.

Reference:
Downey, A. (2015). Think Python: How to Think Like a Computer Scientist. Green Tea Press.
[https://greenteapress.com/thinkpython2/thinkpython2.pdf](https://greenteapress.com/
thinkpython2/thinkpython2.pdf)

You might also like