From b33138912f316439cc2b8602ff4b5f1c6da53262 Mon Sep 17 00:00:00 2001 From: PaulinaMamiaga <228375023+PaulinaMamiaga@users.noreply.github.com> Date: Sun, 28 Dec 2025 19:06:36 +0100 Subject: [PATCH 1/7] update --- lab-python-oop.ipynb | 92 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 86 insertions(+), 6 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index c13bc58..beeb765 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,21 +56,101 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 3, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Creating bank accounts and performing transactions:\n", + "Account 1 Number: 1\n", + "Account 2 Number: 2\n", + "Deposited: 100. New balance: 100\n", + "Withdrew: 50. New balance: 50\n", + "Account 1 Balance: 50\n", + "Deposited: 200. New balance: 200\n", + "Insufficient funds.\n", + "Account 2 Balance: 200\n" + ] + } + ], "source": [ "# your code goes here\n", + "\n", + "# Create a BankAccount class with the following attributes and methods:\n", + "# Attributes: account_number (a unique identifier for the bank account), balance (the current balance of the account. By default, is 0)\n", + "# Methods: deposit(amount) - adds the specified amount to the account balance, withdraw(amount) - subtracts the specified amount from the account balance,\n", + "# get_balance() - returns the current balance of the account, get_account_number() - returns the account number of the account\n", + "# Test the class by creating a few instances of BankAccount and making deposits and withdrawals.\n", + "# Ensure that the account_number attribute is unique for each instance of BankAccount.\n", + "# Hint: create a class attribute account_count to keep track of the total number of bank accounts that have been created using the BankAccount class.\n", + "\n", + "class BankAccount:\n", + " account_count = 0 # Class attribute to keep track of the number of accounts\n", + "\n", + " def __init__(self):\n", + " BankAccount.account_count += 1\n", + " self.account_number = BankAccount.account_count # Unique account number\n", + " self.balance = 0 # Default balance\n", + "\n", + " def deposit(self, amount):\n", + " if amount > 0:\n", + " self.balance += amount\n", + " print(f\"Deposited: {amount}. New balance: {self.balance}\")\n", + " else:\n", + " print(\"Deposit amount must be positive.\")\n", + "\n", + " def withdraw(self, amount):\n", + " if amount > 0:\n", + " if amount <= self.balance:\n", + " self.balance -= amount\n", + " print(f\"Withdrew: {amount}. New balance: {self.balance}\")\n", + " else:\n", + " print(\"Insufficient funds.\")\n", + " else:\n", + " print(\"Withdrawal amount must be positive.\")\n", + "\n", + " def get_balance(self):\n", + " return self.balance\n", + "\n", + " def get_account_number(self):\n", + " return self.account_number\n", + "print(\"Creating bank accounts and performing transactions:\") \n", + "\n", + "# Test the BankAccount class\n", + "account1 = BankAccount()\n", + "account2 = BankAccount()\n", + "print(f\"Account 1 Number: {account1.get_account_number()}\")\n", + "print(f\"Account 2 Number: {account2.get_account_number()}\")\n", + "account1.deposit(100)\n", + "account1.withdraw(50)\n", + "print(f\"Account 1 Balance: {account1.get_balance()}\")\n", + "account2.deposit(200)\n", + "account2.withdraw(300) # Should show insufficient funds\n", + "print(f\"Account 2 Balance: {account2.get_balance()}\")\n", "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "id": "ee789466-d4cf-4dd8-b742-6863d42c3e5c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "ename": "TypeError", + "evalue": "BankAccount.__init__() takes 1 positional argument but 2 were given", + "output_type": "error", + "traceback": [ + "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", + "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", + "Cell \u001b[1;32mIn[6], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Testing the BankAccount class\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;66;03m# Creating two instances of the BankAccount class with initial balances of 1000 and 500\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m account1 \u001b[38;5;241m=\u001b[39m BankAccount(\u001b[38;5;241m1000\u001b[39m)\n\u001b[0;32m 4\u001b[0m account2 \u001b[38;5;241m=\u001b[39m BankAccount(\u001b[38;5;241m500\u001b[39m)\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAccount 1 balance:\u001b[39m\u001b[38;5;124m\"\u001b[39m, account1\u001b[38;5;241m.\u001b[39mget_balance()) \u001b[38;5;66;03m# This should print 1000\u001b[39;00m\n", + "\u001b[1;31mTypeError\u001b[0m: BankAccount.__init__() takes 1 positional argument but 2 were given" + ] + } + ], "source": [ "# Testing the BankAccount class\n", "# Creating two instances of the BankAccount class with initial balances of 1000 and 500\n", @@ -245,7 +325,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "base", "language": "python", "name": "python3" }, @@ -259,7 +339,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4, From fe327f3d7de2da0c17276b324f4c82ff83ca468a Mon Sep 17 00:00:00 2001 From: PaulinaMamiaga <228375023+PaulinaMamiaga@users.noreply.github.com> Date: Sun, 28 Dec 2025 19:23:04 +0100 Subject: [PATCH 2/7] update --- lab-python-oop.ipynb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index beeb765..78876c2 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,7 +56,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 7, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, "outputs": [ @@ -71,7 +71,7 @@ "Withdrew: 50. New balance: 50\n", "Account 1 Balance: 50\n", "Deposited: 200. New balance: 200\n", - "Insufficient funds.\n", + "Insufficient funds. Current balance: 200, attempted withdrawal: 300\n", "Account 2 Balance: 200\n" ] } @@ -108,7 +108,8 @@ " self.balance -= amount\n", " print(f\"Withdrew: {amount}. New balance: {self.balance}\")\n", " else:\n", - " print(\"Insufficient funds.\")\n", + " print(f\"Insufficient funds. Current balance: {self.balance}, attempted withdrawal: {amount}\")\n", + "\n", " else:\n", " print(\"Withdrawal amount must be positive.\")\n", "\n", From 900315f5ff0f7b36a31f28640b50070f7f16f82c Mon Sep 17 00:00:00 2001 From: PaulinaMamiaga <228375023+PaulinaMamiaga@users.noreply.github.com> Date: Sun, 28 Dec 2025 19:38:11 +0100 Subject: [PATCH 3/7] update --- lab-python-oop.ipynb | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index 78876c2..3736383 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,7 +56,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 14, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, "outputs": [ @@ -67,11 +67,11 @@ "Creating bank accounts and performing transactions:\n", "Account 1 Number: 1\n", "Account 2 Number: 2\n", - "Deposited: 100. New balance: 100\n", - "Withdrew: 50. New balance: 50\n", + "[Account 1] Deposited: 100. New balance: 100\n", + "[Account 1] Withdrew: 50. New balance: 50\n", "Account 1 Balance: 50\n", - "Deposited: 200. New balance: 200\n", - "Insufficient funds. Current balance: 200, attempted withdrawal: 300\n", + "[Account 2] Deposited: 200. New balance: 200\n", + "[Account 2] Insufficient funds. Current balance: 200, attempted withdrawal: 300\n", "Account 2 Balance: 200\n" ] } @@ -98,20 +98,23 @@ " def deposit(self, amount):\n", " if amount > 0:\n", " self.balance += amount\n", - " print(f\"Deposited: {amount}. New balance: {self.balance}\")\n", + " print(f\"[Account {self.account_number}] Deposited: {amount}. New balance: {self.balance}\")\n", " else:\n", - " print(\"Deposit amount must be positive.\")\n", + " print(f\"[Account {self.account_number}] Deposit amount must be positive.\")\n", "\n", " def withdraw(self, amount):\n", " if amount > 0:\n", " if amount <= self.balance:\n", " self.balance -= amount\n", - " print(f\"Withdrew: {amount}. New balance: {self.balance}\")\n", + " print(f\"[Account {self.account_number}] Withdrew: {amount}. New balance: {self.balance}\")\n", " else:\n", - " print(f\"Insufficient funds. Current balance: {self.balance}, attempted withdrawal: {amount}\")\n", + " print(\n", + " f\"[Account {self.account_number}] Insufficient funds. \"\n", + " f\"Current balance: {self.balance}, attempted withdrawal: {amount}\"\n", + " )\n", "\n", " else:\n", - " print(\"Withdrawal amount must be positive.\")\n", + " print(f\"[Account {self.account_number}] Withdrawal amount must be positive.\")\n", "\n", " def get_balance(self):\n", " return self.balance\n", @@ -136,7 +139,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 15, "id": "ee789466-d4cf-4dd8-b742-6863d42c3e5c", "metadata": {}, "outputs": [ @@ -147,7 +150,7 @@ "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[6], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Testing the BankAccount class\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;66;03m# Creating two instances of the BankAccount class with initial balances of 1000 and 500\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m account1 \u001b[38;5;241m=\u001b[39m BankAccount(\u001b[38;5;241m1000\u001b[39m)\n\u001b[0;32m 4\u001b[0m account2 \u001b[38;5;241m=\u001b[39m BankAccount(\u001b[38;5;241m500\u001b[39m)\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAccount 1 balance:\u001b[39m\u001b[38;5;124m\"\u001b[39m, account1\u001b[38;5;241m.\u001b[39mget_balance()) \u001b[38;5;66;03m# This should print 1000\u001b[39;00m\n", + "Cell \u001b[1;32mIn[15], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Testing the BankAccount class\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;66;03m# Creating two instances of the BankAccount class with initial balances of 1000 and 500\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m account1 \u001b[38;5;241m=\u001b[39m BankAccount(\u001b[38;5;241m1000\u001b[39m)\n\u001b[0;32m 4\u001b[0m account2 \u001b[38;5;241m=\u001b[39m BankAccount(\u001b[38;5;241m500\u001b[39m)\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAccount 1 balance:\u001b[39m\u001b[38;5;124m\"\u001b[39m, account1\u001b[38;5;241m.\u001b[39mget_balance()) \u001b[38;5;66;03m# This should print 1000\u001b[39;00m\n", "\u001b[1;31mTypeError\u001b[0m: BankAccount.__init__() takes 1 positional argument but 2 were given" ] } From 3cc24c9802a777efdbea3e9b161d8f1fde4cacd3 Mon Sep 17 00:00:00 2001 From: PaulinaMamiaga <228375023+PaulinaMamiaga@users.noreply.github.com> Date: Sun, 28 Dec 2025 19:45:53 +0100 Subject: [PATCH 4/7] update --- lab-python-oop.ipynb | 73 +++++++++++++++++++++++++++++++++++--------- 1 file changed, 59 insertions(+), 14 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index 3736383..3362029 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,7 +56,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 18, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, "outputs": [ @@ -139,19 +139,64 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 19, + "id": "0abf5db3", + "metadata": {}, + "outputs": [], + "source": [ + "class BankAccount:\n", + " account_count = 0\n", + "\n", + " def __init__(self, balance=0): # allow initial balance\n", + " BankAccount.account_count += 1\n", + " self.account_number = BankAccount.account_count\n", + " self.balance = balance # set initial balance\n", + "\n", + " def deposit(self, amount):\n", + " if amount > 0:\n", + " self.balance += amount\n", + " print(f\"[Account {self.account_number}] Deposited: {amount}. New balance: {self.balance}\")\n", + " else:\n", + " print(f\"[Account {self.account_number}] Deposit amount must be positive.\")\n", + "\n", + " def withdraw(self, amount):\n", + " if amount > 0:\n", + " if amount <= self.balance:\n", + " self.balance -= amount\n", + " print(f\"[Account {self.account_number}] Withdrew: {amount}. New balance: {self.balance}\")\n", + " else:\n", + " print(f\"[Account {self.account_number}] Insufficient funds. Balance: {self.balance}, attempted: {amount}\")\n", + " else:\n", + " print(f\"[Account {self.account_number}] Withdrawal amount must be positive.\")\n", + "\n", + " def get_balance(self):\n", + " return self.balance\n", + "\n", + " def get_account_number(self):\n", + " return self.account_number\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 20, "id": "ee789466-d4cf-4dd8-b742-6863d42c3e5c", "metadata": {}, "outputs": [ { - "ename": "TypeError", - "evalue": "BankAccount.__init__() takes 1 positional argument but 2 were given", - "output_type": "error", - "traceback": [ - "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", - "Cell \u001b[1;32mIn[15], line 3\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[38;5;66;03m# Testing the BankAccount class\u001b[39;00m\n\u001b[0;32m 2\u001b[0m \u001b[38;5;66;03m# Creating two instances of the BankAccount class with initial balances of 1000 and 500\u001b[39;00m\n\u001b[1;32m----> 3\u001b[0m account1 \u001b[38;5;241m=\u001b[39m BankAccount(\u001b[38;5;241m1000\u001b[39m)\n\u001b[0;32m 4\u001b[0m account2 \u001b[38;5;241m=\u001b[39m BankAccount(\u001b[38;5;241m500\u001b[39m)\n\u001b[0;32m 6\u001b[0m \u001b[38;5;28mprint\u001b[39m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mAccount 1 balance:\u001b[39m\u001b[38;5;124m\"\u001b[39m, account1\u001b[38;5;241m.\u001b[39mget_balance()) \u001b[38;5;66;03m# This should print 1000\u001b[39;00m\n", - "\u001b[1;31mTypeError\u001b[0m: BankAccount.__init__() takes 1 positional argument but 2 were given" + "name": "stdout", + "output_type": "stream", + "text": [ + "Account 1 balance: 1000\n", + "Account 1 number: 1\n", + "Account 2 balance: 500\n", + "Account 2 number: 2\n", + "[Account 1] Deposited: 500. New balance: 1500\n", + "[Account 1] Withdrew: 200. New balance: 1300\n", + "Account 1 balance after transactions: 1300\n", + "[Account 2] Insufficient funds. Balance: 500, attempted: 600\n", + "Account 2 balance after transactions: 500\n" ] } ], @@ -201,7 +246,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 21, "id": "4f8848b5-05d3-4259-9e24-914537926778", "metadata": {}, "outputs": [], @@ -235,7 +280,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 22, "id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30", "metadata": {}, "outputs": [], @@ -273,7 +318,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 23, "id": "3c883c6e-3cb8-4043-92d3-12409668a28e", "metadata": {}, "outputs": [], @@ -318,7 +363,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 24, "id": "faa5b148-c11b-4be0-b810-de8a7da81451", "metadata": {}, "outputs": [], From f02b84095557bf108e6cb7c512e18b426177354d Mon Sep 17 00:00:00 2001 From: PaulinaMamiaga <228375023+PaulinaMamiaga@users.noreply.github.com> Date: Sun, 28 Dec 2025 20:07:16 +0100 Subject: [PATCH 5/7] update --- lab-python-oop.ipynb | 48 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 45 insertions(+), 3 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index 3362029..e28697e 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,7 +56,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 26, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, "outputs": [ @@ -79,13 +79,14 @@ "source": [ "# your code goes here\n", "\n", + "# Version 1\n", + "\n", "# Create a BankAccount class with the following attributes and methods:\n", "# Attributes: account_number (a unique identifier for the bank account), balance (the current balance of the account. By default, is 0)\n", "# Methods: deposit(amount) - adds the specified amount to the account balance, withdraw(amount) - subtracts the specified amount from the account balance,\n", "# get_balance() - returns the current balance of the account, get_account_number() - returns the account number of the account\n", "# Test the class by creating a few instances of BankAccount and making deposits and withdrawals.\n", "# Ensure that the account_number attribute is unique for each instance of BankAccount.\n", - "# Hint: create a class attribute account_count to keep track of the total number of bank accounts that have been created using the BankAccount class.\n", "\n", "class BankAccount:\n", " account_count = 0 # Class attribute to keep track of the number of accounts\n", @@ -137,13 +138,43 @@ "\n" ] }, + { + "cell_type": "markdown", + "id": "852e997f", + "metadata": {}, + "source": [ + "**Note for Version 1 (default balance = 0, no initial balance parameter):**\n", + ">\n", + "*Note:*\n", + ">This implementation follows the original requirement strictly: each BankAccount is created with a default balance of 0 and the account number is automatically generated using the account_count class attribute. A limitation of this version is that it does not allow setting an initial balance at instantiation, so any starting funds must be added later using deposit()." + ] + }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 27, "id": "0abf5db3", "metadata": {}, "outputs": [], "source": [ + "# your code goes here\n", + "\n", + "# Version 2\n", + "\n", + "# Create a BankAccount class with the following attributes and methods:\n", + "# Attributes:\n", + "# - account_number: a unique identifier for the bank account\n", + "# - balance: the current balance of the account, which can be set at initialization (default is 0)\n", + "#\n", + "# Methods:\n", + "# - deposit(amount): adds the specified amount to the account balance\n", + "# - withdraw(amount): subtracts the specified amount from the account balance if sufficient funds are available\n", + "# - get_balance(): returns the current balance of the account\n", + "# - get_account_number(): returns the account number of the account\n", + "#\n", + "# Ensure that the account_number attribute is unique for each instance of BankAccount.\n", + "\n", + "\n", + "\n", "class BankAccount:\n", " account_count = 0\n", "\n", @@ -178,6 +209,17 @@ "\n" ] }, + { + "cell_type": "markdown", + "id": "1c97d6cc", + "metadata": {}, + "source": [ + "**Note for Version 2 (supports initial balance):**\n", + ">\n", + "*Note:*\n", + "> This implementation extends the original requirement by allowing an optional initial balance through __init__(self, balance=0). This makes it compatible with the lab’s provided testing code, which creates accounts with starting balances (e.g., BankAccount(1000)). The trade-off is that it slightly goes beyond the “balance is 0 by default” statement, although it still respects it (the default remains 0 when no value is passed)." + ] + }, { "cell_type": "code", "execution_count": 20, From 22726dcf9beb1ed35e614b6406ece39b35b34395 Mon Sep 17 00:00:00 2001 From: PaulinaMamiaga <228375023+PaulinaMamiaga@users.noreply.github.com> Date: Sun, 28 Dec 2025 20:43:22 +0100 Subject: [PATCH 6/7] update --- lab-python-oop.ipynb | 60 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 55 insertions(+), 5 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index e28697e..d0a6d43 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -288,12 +288,34 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "id": "4f8848b5-05d3-4259-9e24-914537926778", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "\n", + "# Create a SavingsAccount class that inherits from the BankAccount class\n", + "# Additional attribute:\n", + "# - interest_rate (annual interest rate, default is 0.01)\n", + "# Additional methods:\n", + "# - add_interest() -> adds earned interest to the balance\n", + "# - get_interest_rate() -> returns the interest rate\n", + "\n", + "class SavingsAccount(BankAccount):\n", + "\n", + " def __init__(self, balance=0, interest_rate=0.01):\n", + " # Initialize attributes from the parent class\n", + " super().__init__(balance)\n", + " self.interest_rate = interest_rate\n", + "\n", + " def add_interest(self):\n", + " interest = self.balance * self.interest_rate\n", + " self.balance += interest\n", + " print(f\"[Account {self.account_number}] Added interest: {interest}. New balance: {self.balance}\")\n", + "\n", + " def get_interest_rate(self):\n", + " return self.interest_rate" ] }, { @@ -322,12 +344,40 @@ }, { "cell_type": "code", - "execution_count": 22, + "execution_count": 29, "id": "bccc7f6d-d58c-4909-9314-aaf4afc1cd30", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[Account 1] Deposited: 50. New balance: 150\n", + "[Account 1] Withdrew: 25. New balance: 125\n", + "[Account 1] Added interest: 2.5. New balance: 127.5\n", + "Current balance: 127.5\n", + "Interest rate: 0.02\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "\n", + "# Create a SavingsAccount object with a balance of $100 and interest rate of 2%\n", + "savings_account = SavingsAccount(balance=100, interest_rate=0.02)\n", + "\n", + "# Deposit $50\n", + "savings_account.deposit(50)\n", + "\n", + "# Withdraw $25\n", + "savings_account.withdraw(25)\n", + "\n", + "# Add interest\n", + "savings_account.add_interest()\n", + "\n", + "# Print current balance and interest rate\n", + "print(\"Current balance:\", savings_account.get_balance())\n", + "print(\"Interest rate:\", savings_account.get_interest_rate())" ] }, { From 1ec36d0811f2dd6abfd74102dfc22caa53fa5cac Mon Sep 17 00:00:00 2001 From: PaulinaMamiaga <228375023+PaulinaMamiaga@users.noreply.github.com> Date: Sun, 28 Dec 2025 20:48:59 +0100 Subject: [PATCH 7/7] update --- lab-python-oop.ipynb | 96 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 91 insertions(+), 5 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index d0a6d43..6cd5346 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -410,12 +410,63 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 30, "id": "3c883c6e-3cb8-4043-92d3-12409668a28e", "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "# your code goes here\n", + "\n", + "# Create a CheckingAccount class that inherits from the BankAccount class.\n", + "# Additional attributes:\n", + "# - transaction_fee (fee charged per transaction, default is 1)\n", + "# - transaction_count (number of transactions made in the current month)\n", + "# Additional methods:\n", + "# - deduct_fees(): deducts total transaction fees and resets the transaction count\n", + "# - reset_transactions(): resets the transaction count to 0\n", + "# - get_transaction_count(): returns the current transaction count\n", + "# Note: deposit and withdraw methods are overwritten to track transactions.\n", + "\n", + "class CheckingAccount(BankAccount):\n", + "\n", + " def __init__(self, balance=0, transaction_fee=1):\n", + " super().__init__(balance)\n", + " self.transaction_fee = transaction_fee\n", + " self.transaction_count = 0\n", + "\n", + " def deposit(self, amount):\n", + " if amount > 0:\n", + " self.balance += amount\n", + " self.transaction_count += 1\n", + " print(f\"[Account {self.account_number}] Deposited: {amount}. New balance: {self.balance}\")\n", + " else:\n", + " print(f\"[Account {self.account_number}] Deposit amount must be positive.\")\n", + "\n", + " def withdraw(self, amount):\n", + " if amount > 0:\n", + " if amount <= self.balance:\n", + " self.balance -= amount\n", + " self.transaction_count += 1\n", + " print(f\"[Account {self.account_number}] Withdrew: {amount}. New balance: {self.balance}\")\n", + " else:\n", + " print(f\"[Account {self.account_number}] Insufficient funds.\")\n", + " else:\n", + " print(f\"[Account {self.account_number}] Withdrawal amount must be positive.\")\n", + "\n", + " def deduct_fees(self):\n", + " total_fees = self.transaction_count * self.transaction_fee\n", + " if total_fees <= self.balance:\n", + " self.balance -= total_fees\n", + " print(f\"Transaction fees of {total_fees}$ have been deducted from your account balance.\")\n", + " self.reset_transactions()\n", + " else:\n", + " print(\"Insufficient balance to deduct transaction fees.\")\n", + "\n", + " def reset_transactions(self):\n", + " self.transaction_count = 0\n", + "\n", + " def get_transaction_count(self):\n", + " return self.transaction_count" ] }, { @@ -455,12 +506,47 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": 31, "id": "faa5b148-c11b-4be0-b810-de8a7da81451", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[Account 2] Deposited: 100. New balance: 600\n", + "[Account 2] Withdrew: 50. New balance: 550\n", + "Transaction fees of 4$ have been deducted from your account balance.\n", + "Current balance: 546\n", + "Transaction count: 0\n", + "[Account 2] Deposited: 200. New balance: 746\n", + "[Account 2] Withdrew: 75. New balance: 671\n", + "Transaction fees of 4$ have been deducted from your account balance.\n", + "Current balance: 667\n", + "Transaction count: 0\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "\n", + "# Testing the CheckingAccount class\n", + "\n", + "checking_account = CheckingAccount(balance=500, transaction_fee=2)\n", + "\n", + "checking_account.deposit(100)\n", + "checking_account.withdraw(50)\n", + "checking_account.deduct_fees()\n", + "\n", + "print(\"Current balance:\", checking_account.get_balance())\n", + "print(\"Transaction count:\", checking_account.get_transaction_count())\n", + "\n", + "checking_account.deposit(200)\n", + "checking_account.withdraw(75)\n", + "checking_account.deduct_fees()\n", + "\n", + "print(\"Current balance:\", checking_account.get_balance())\n", + "print(\"Transaction count:\", checking_account.get_transaction_count())\n" ] } ],