built a bank program using python

[removed]

76 comments · 99% upvoted · source · other discussions

comments

Sea-Ad7805

Run the program in Memory Graph Web Debugger%0A%0Adef%20deposit()%3A%0A%20%20%20%20amount%20%3D%20float(input('enter%20amount%20to%20be%20deposited%3A%20'))%0A%20%20%20%20if%20amount%20%3C%200%3A%0A%20%20%20%20%20%20%20%20print('enter%20a%20valid%20amount')%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20return%20amount%0A%0Adef%20withdraw()%3A%0A%20%20%20%20amount%20%3D%20float(input('enter%20amount%20to%20be%20withdrawn%3A%20'))%0A%20%20%20%20if%20amount%20%3E%20balance%3A%0A%20%20%20%20%20%20%20%20print('insufficent%20funds')%0A%20%20%20%20%20%20%20%20return%200%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20return%20amount%0A%0Adef%20show_balance()%3A%0A%20%20%20%20print(f'your%20balance%20is%20%24%7Bbalance%3A.2f%7D')%0A%0Abalance%20%3D%200%0Ais_running%20%3D%20True%0A%0Awhile%20is_running%3A%0A%20%20%20%20print('1.%20deposit')%0A%20%20%20%20print('2.%20balance')%0A%20%20%20%20print('3.%20withdraw')%0A%20%20%20%20print('4.%20exit')%0A%20%20%20%20choice%20%3D%20input('enter%20your%20choice(1%2C2%2C3%2C4)%3A%20')%0A%0A%20%20%20%20if%20choice%20%3D%3D%20'1'%3A%0A%20%20%20%20%20%20%20%20balance%20%2B%3D%20deposit()%0A%20%20%20%20elif%20choice%20%3D%3D%20'2'%3A%0A%20%20%20%20%20%20%20%20show_balance()%0A%20%20%20%20elif%20choice%20%3D%3D%20'3'%3A%0A%20%20%20%20%20%20%20%20balance%20-%3D%20withdraw()%0A%20%20%20%20elif%20choice%20%3D%3D%20'4'%3A%0A%20%20%20%20%20%20%20%20is_running%20%3D%20False%0A%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20print(%22What%3F%22)&play).

Crazymonkey200

Nice. Could go further by starting experimenting saving data with a text file at first, maybe even setting up a small database. Also it's good to learn object-oriented programming with a program like this, making user and bank classes and trying to interact with objects is a good way to get into it.

SharpTradition8748

First time OOP made any sort of sense to me was with a small “bank” project, good advice!

yinkeys

Thumbs up

Binary101010

1) User input should be taken (and validated) separately from the functions that do something with that user input.

2) I don't really like the fact that withdraw() always returns a number, but deposit() can return a number or None if the input was invalid. Function return values should ideally only be a single type. (The best way to fix this is to not validate the input in the same function but do that separately.)

nkCOD

Is it a good option to check the output data using try-except ? I just did this - I made an endless loop through while, then I threw the input lines into trying try-except, then, if I needed a number, I translated the entered data into the numeric int() format. In case of an error, we switched to except, where we were greeted by print(), and then continue. The loop was stopped using break if the input was correct

Unequivalent_Balance

Nice work. I’m just wondering why it won’t let me take out all of my money ;)

cloud2ground

Not your money any more!

Complete_Law9527

I like it. Easy to read and understand.

bradland

Here's a fun next step: Add a persistence layer.

Right now, the app starts over from scratch every time you run the app. Start by figuring out a way to save the current balance between sessions.

Once you've got that working, work on figuring out how to keep all transactions. Think about what you'd want to save with the transaction. What about the date and time? What will you store it in? A file? Maybe a SQLite database?

Once we have a history of transactions, what else can we do? Maybe in addition to seeing the current balance, we can query the balance on a particular date? What about listing all transactions for a particular date?

da-one-n-only

I just started to get in to python. I can't wait to be like you guys knowing what I'm looking at.

Dapper-Ad8945

Same here

PoussinVermillon

wheres the part where the money magically disappears the instant it is deposited in the account ? /j, good job

Ambivalent-Mammal

Overall, well done, but you need a valid return value for negative deposits (unless None casts to 0).

I imagine the really interesting version of this problem will come in the advanced class with joint checking accounts, and concurrency.

mayank_0508

great job

Janeson81

Biggest problem I see is that if you use deposit() there's nothing limiting the user input so they might say they want to deposit "Hello" money

This can be fixed by using try: ... except <exception>: ... blocks. It follows instructions normally in the try block, but when the interpreter finds a specified error (for example a ValueError, the one you get if you try to float("hi")) it jumps straight to the except block and carries on

The proper structure should be: try: ... ... ... except ValueError: print("enter a valid number")

thuiop1

I am not sure how 39 comments fail to mention that you should have variables as input of your functions instead of using global variables.

Simple-Olive895

It's been a while since I did any python coding, but I'm pretty sure you should still type validate in python? Like what happends if someone selects deposit and enters: "one million"?

Trying to cast that to a float will crash. So you should guard yourself with try-catch when you accept user input.

Prestigious_Long2691

Nice Python Script

Stretchslash

If you are doing a bunch of if statements going if (choice == 1)else if (choice == 2)... ect

A very simple change would be using a switch case instead. I think it uses match in python but similar

match choice case 1: // Do logic case 2: // Do logic case _: // Do default logic

[deleted]

[removed]

TheProv1

This is a good idea

Do you mind explaining what do you mean exactly - geeksforgeeks is good

Lemon-Emu

Looks good. What happens if you try and withdraw a negative number though?

Kevdog824_

Banks hate this one simple trick!!

Fit_Lingonberry_2494

Try with OOP

Kevdog824_

The easiest improvement you can make is refactoring your functions so they get input from caller as arguments rather than relying on global state

MJ12_2802

INSUFFICIENT FUNDS?? That's impossible... I still have checks!

Due_Faithlessness458

Try a switch for input management, you could also use a single function for managing the amount, make a func that receives a string with the operation and returns the value, so you could make something like this: switch choice: case “1”: balance+=requestAmount(“deposited”) … case “3”: request the amount check for valid transaction …

and define requestAmount(str op) return the same but introduce a variable to the string of the message

brutalbombs

Good stuff.

mr_frpdo

The biggest thing i see is to not use floats as the data type for money. You should either store as cents and format display to decimal or use the Decimal class. Floats should never be used for money due to floating point accuracy issues.

Particular_Scale_881

OP try deposit 0.1 and 0.2 and print the full amount variable you will notice that 0.0...4 cent are created

godlikedk

Never save money as float

aronsajan

The withdraw() and deposit() respectively supposed to reduce or add an amount to the balance. However, you are doing that outside these function by doing that operation in the main loop. You withdraw() and deposit() are just doing validation of the input, it would be better if the balance gets adjusted within these functions.

SmthnsmthnDngerzone

its not cobol try again lol

dpmlss

No entiendo nada pero algún día si 😌

CommitteeKey4381

Lol, 😆 using JS/TS would have been better

Awkward_Climate_579

Can you please explain where and whats the use of the deposite variable and its value Love the work.

c0lpan1c

Good start hombre! Next do a post office routing program. I think there’s a hacker rank problem with something similar.

I_am_Noro04

Make a UI using tkinter or curses

jagermeister_master

u can withdraw negative money

Parking-Economics-68

For financial operations it's better to use decimal type instead of float. Float number could cause approximation errors

Major-Incident-8650

Use OOP. You will compact the code and make it easier to have control over

SwimmerOld6155

Function definitions should be before anything else (before that print statement), even though they are allowed to appear anyway because Python is read line by line. I would want line breaks between the function definitions, and between is_running and the while loop, just personal preference, good job!

Significant_Ad7286

Nice! You can also now go on to implement a database to store records so that the funds are not just limited to one terminal session and once you're done with that, try to explore the UI libraries of Python, like Tkinter, if you're into it

L3RightMC

I did it with C. It had the same functions and everything, and well, it works perfectly.

TheProv1

Great work, as for the suggestions

Improve upon the menu design - also if possible try to use MySQL with this so that you can make it like a proper management system

Next add more features like accounts etc

t1nk3rz3r0-

Ncie one so far, Adding screen function would be smooth tho!

sandar6

i have no money

TheDebonairCorey

Solid start, but you're mixing user input with your logic functions which will make them harder to test and reuse later.

Smoke_along

So depositing 0 doesn’t give a faulty?

SteadyGrowth_

Well done, easy to read et understand

PeaFast3114

Brocode?

[deleted]

[removed]

PythonLearning-ModTeam

Quality posts only

No_Preference_6923

Your project is good, but it would be even better if you added some advanced features. For example, you could include an option to create a new bank account with a unique account number for each user. You can also add a money transfer section where users can transfer funds using the sender’s account number, receiver’s account number, and IFSC code. Additionally, including a transaction history section would make the project feel more like a real bank management system. Many students usually make projects with only basic features like deposit, withdraw, display details, and exit, so adding these extra functionalities would make your project stand out and look more professional. Overall, your project idea is good, and with these improvements, it can become much more practical, interactive, and impressive.

Embarrassed_Ebb6506

Good

pontz

For a beginner and basic functionality standpoint I think you have a good base here.

I would say instead of doing if/elif/elif setup a dictionary and call it that way.

Also you don’t have any error handling or input sanitizing like what if they put in “$12.05” or try and put “10 dollars” or if they try to withdraw .001?

OpportunityFun6969

Hook it up with mysql or mariaDB and keep a small db of accounts and their balances. You could add in account creation too for new entries

Ok_Butterscotch_7930

very cool, try with classes next. You'll see why classes are so powerful

ittology

Nice work. A bank program is actually a good beginner project because it forces you to think about user input, validation, state, and edge cases.

Some general next steps I’d suggest:

- add proper input validation so invalid text or negative amounts don’t break the flow

- save the balance/transactions somewhere so the data is still there after restarting the program

- add a simple transaction history

- later, try rewriting it with a BankAccount class to practice OOP

- if you work with money values, look into Decimal or storing cents as integers instead of using floats

Good project to keep expanding step by step.

ExtremeVick

Prompt pls😼

Fantastic-Day-69

Cute. Is it your first script ?