From d41498e790ec1aede13cb8314103443e30c5eec3 Mon Sep 17 00:00:00 2001 From: samyak-anand Date: Mon, 14 Apr 2025 09:54:46 +0200 Subject: [PATCH] task1 --- lab-python-oop.ipynb | 127 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 122 insertions(+), 5 deletions(-) diff --git a/lab-python-oop.ipynb b/lab-python-oop.ipynb index c13bc58..ee3fdf8 100644 --- a/lab-python-oop.ipynb +++ b/lab-python-oop.ipynb @@ -56,21 +56,138 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 7, "id": "21625526-3fae-4c55-bab5-f91940070681", "metadata": {}, "outputs": [], "source": [ "# your code goes here\n", + "class BankAccount:\n", + "\n", + " account_count =0\n", + " def __init__(self):\n", + " BankAccount.account_count +=1 \n", + " self.account_number = BankAccount.account_count \n", + " self.balance = 0\n", + "\n", + "\n", + " def deposit(self, amount):\n", + " if amount > 0:\n", + " self.balance += amount\n", + " print(f\"Deposited ${amount}. New balance is ${self.balance}\")\n", + " else:\n", + " print(\"Invalid deposit amount.\")\n", + " \n", + " \n", + " def withdraw(self, amount):\n", + " if 0 < amount <= self.balance:\n", + " self.balance -= amount\n", + " print(f\"Withdrew ${amount}. New balance is ${self.balance}\")\n", + " else:\n", + " print(\"Invalid withdrawal amount.\")\n", + "\n", + " \n", + " def get_balance(self):\n", + " return self.balance\n", + " \n", + " \n", + "\n", + " def get_account_number(self):\n", + " return self.account_number\n", + " \n", + "\n", + " \n", "\n" ] }, { "cell_type": "code", - "execution_count": null, + "execution_count": 8, + "id": "d0e3f369", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Deposited $1000. New balance is $1000\n", + "Withdrew $200. New balance is $800\n", + "Account #1 Balance: $800\n" + ] + } + ], + "source": [ + "account1 = BankAccount()\n", + "account1.deposit(1000)\n", + "account1.withdraw(200)\n", + "print(f\"Account #{account1.get_account_number()} Balance: ${account1.get_balance()}\")\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "9ed6e6e0", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Deposited $500. New balance is $500\n", + "Invalid withdrawal amount.\n", + "Account #2 Balance: $500\n" + ] + } + ], + "source": [ + "account2 = BankAccount()\n", + "account2.deposit(500)\n", + "account2.withdraw(1000) \n", + "print(f\"Account #{account2.get_account_number()} Balance: ${account2.get_balance()}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "id": "c3888deb", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Deposited $1200. New balance is $1200\n", + "Withdrew $300. New balance is $900\n", + "Account #3 Balance: $900\n" + ] + } + ], + "source": [ + "account3 = BankAccount()\n", + "account3.deposit(1200)\n", + "account3.withdraw(300)\n", + "\n", + "print(f\"Account #{account3.get_account_number()} Balance: ${account3.get_balance()}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 11, "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[31m---------------------------------------------------------------------------\u001b[39m", + "\u001b[31mTypeError\u001b[39m Traceback (most recent call last)", + "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[11]\u001b[39m\u001b[32m, line 3\u001b[39m\n\u001b[32m 1\u001b[39m \u001b[38;5;66;03m# Testing the BankAccount class\u001b[39;00m\n\u001b[32m 2\u001b[39m \u001b[38;5;66;03m# Creating two instances of the BankAccount class with initial balances of 1000 and 500\u001b[39;00m\n\u001b[32m----> \u001b[39m\u001b[32m3\u001b[39m account1 = \u001b[43mBankAccount\u001b[49m\u001b[43m(\u001b[49m\u001b[32;43m1000\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[32m 4\u001b[39m account2 = BankAccount(\u001b[32m500\u001b[39m)\n\u001b[32m 6\u001b[39m \u001b[38;5;28mprint\u001b[39m(\u001b[33m\"\u001b[39m\u001b[33mAccount 1 balance:\u001b[39m\u001b[33m\"\u001b[39m, account1.get_balance()) \u001b[38;5;66;03m# This should print 1000\u001b[39;00m\n", + "\u001b[31mTypeError\u001b[39m: 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 +362,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": ".venv", "language": "python", "name": "python3" }, @@ -259,7 +376,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.3" } }, "nbformat": 4,