
For todays Python exercises I wanted to do something with classes. I know the basics about them from CS50 Python, but haven’t used them since that, so it’s fair to say I am a total beginner. To practice this ChatGPT gave me a Banking System exercise (see next paragraph). Below that as always I share with you my code
Exercise: Simple Banking System
Create a basic banking system.
Allow users to create a new account, deposit money, withdraw money, and check their balance.
Use classes to represent users and accounts.
My Code
Since I’m not very familiar with classes I was only able to do a first part of this exercise today. I decided to just make it work with one account. I’ll still need to implement the bank and more features in the coming days 🙂
importlogginglogging.basicConfig(level=logging.DEBUG)classAccount:def__init__(self,account_id,holder_name,initial_balance=0):self.account_id=account_idself.holder_name=holder_nameself.balance=initial_balancedefdeposit(self,amount):ifamount>0:self.balance+=amountlogging.info(f"Deposited{amount}. New balance:{self.balance}")else:logging.warning("Invalid deposit amount.")defwithdraw(self,amount):if0<amount<=self.balance:self.balance-=amountlogging.info(f"Withdrew{amount}. New balance:{self.balance}")else:logging.warning("Invalid withdrawal amount or insufficient funds.")defget_balance(self):returnself.balanceclassBank:...defmain():account=Account(123,"John Doe",50)# Creating an account with initial balance 50try:whileTrue:user_input=input('What would you like to do? Options: n = new account (currently not available), d = deposit money, w = withdraw money, c = check balance, e = exit\n')ifuser_input=='n':logging.info('This feature is not yet avaiable. Please wait for an update in the coming days')elifuser_input=='d':amount=float(input('Enter deposit amount:'))account.deposit(amount)elifuser_input=='w':amount=float(input('Enter withdrawal amount:'))account.withdraw(amount)elifuser_input=='c':print(f"Current balance:{account.get_balance()}")elifuser_input=='e':breakelse:logging.info("Invalid option selected.")exceptKeyboardInterrupt:logging.error('KeyboardInterrupt')exceptValueError:logging.error("Invalid input. Please enter a number.")logging.debug('main() end')if__name__=='__main__':main()
Top comments(0)
For further actions, you may consider blocking this person and/orreporting abuse