Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Python 27: Banking System (part 1)
Gregor Schafroth
Gregor Schafroth

Posted on

Python 27: Banking System (part 1)

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()
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

AI Entrepreneur, building AI Chatbot Assistants for companies and websites.
  • Location
    Zürich, Switzerland
  • Education
    University of St.Gallen, Switzerland
  • Work
    Entrepreneur
  • Joined

More fromGregor Schafroth

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp