Hi guys i need some help i am tryig to code a python fake bank system. The program needs to be able to deposit withdraw,check the balance and quit the program however my code doesnt seem to be working for some reason can someone please help me. Please find attached the code
Jamshaid _1 0 Newbie Poster
Edited by Jamshaid _1
Dani AI
Generated
Brief summary and diagnosis (thread context: , , )
The sample posted in this thread has a few recurring problems: mixing an account balance with a separate money variable, reassigning a function name to a variable (shadowing), incorrect updates to the balance, and unvalidated use of input() that can either evaluate or raise errors depending on Python version. The simplest, most robust approach is to treat the account balance as the single source of truth, validate numeric input, and always return-and-reassign the updated balance (for example balance = deposit(balance)), never assign a function object to a numeric variable.
Concrete, practical fixes to apply
- Use a single
balancevariable and clear function responsibilities:deposit(balance) -> new_balance,withdraw(balance) -> new_balance,show_balance(balance). - Validate inputs with try/except and reject non-numeric or negative amounts.
- When dealing with money, prefer
decimal.Decimalto avoid floating-point rounding. - Do not reuse function names as variables (for example avoid
deposit = deposit(...)). - Handle insufficient funds in
withdrawbefore subtracting.
Example (Python 3, Decimal-based, minimal structure)
from decimal import Decimal, InvalidOperation
def get_amount(prompt):
s = input(prompt).strip()
try:
amt = Decimal(s)
except (InvalidOperation, ValueError):
print("Enter a valid number.")
return None
if amt <= 0:
print("Amount must be positive.")
return None
return amt
def deposit(balance):
amt = get_amount("Deposit amount: $")
return balance + amt if amt is not None else balance
def withdraw(balance):
amt = get_amount("Withdraw amount: $")
if amt is None:
return balance
if amt > balance:
print("Insufficient funds.")
return balance
return balance - amt
balance = Decimal("0.00")
while True:
print("\n1) Deposit 2) Withdraw 3) Balance 4) Quit")
cmd = input("> ").strip()
if cmd == "1":
balance = deposit(balance)
elif cmd == "2":
balance = withdraw(balance)
elif cmd == "3":
print("Balance: $", balance)
elif cmd == "4":
break
else:
print("Invalid choice.") Final notes
Prefer raw_input() and classic print syntax if running Python 2, and avoid relying on input() there (it evaluates). Keep variable names explicit (e.g., wallet vs account_balance) if modeling cash outside the bank.
Recommended Answers
Jump to Post— hericles 289Please add your code to the post, i.e. in the post, not an attachment.
All 2 Replies
hericles 289 Master Poster Featured Poster
Please add your code to the post, i.e. in the post, not an attachment.
Adam_40 0 Newbie Poster
Seems like a homework task to me :)
Try this, i've wrote you this but try and understand it too.
#THIS PART IS NOT THE CODE ITSELF THESE ARE JUST COMMANDS
def menu():
money = int(5000)
money = float(money)
#print the options you have
print "Welcome to the Python Bank System"
print " "
print "Your Transaction Options Are:"
print "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
print "1) Deposit Money"
print "2) Withdraw Money"
print "3) Check Balance"
print "4) Quit Python Bank System.pyw"
print
return input ("Choose your option: ")
#Here is the deposit part.... This is where the user inputs the amount of money
#they wish to deposit into their account.
def deposit(balance, money):
deposit = input("How much: $")
deposit = float(deposit)
if deposit <= money:
balance = balance + 1
money = money - deposit
money = float(money)
deposit = deposit * .1
deposit = float(deposit)
balance = deposit + balance
balance = float(balance)
print "You've successfully deposited $", deposit, "into your account."
print
bank_balance(balance)
return balance
#This is where the user inputs the amount of money they wish to withdraw from
#their account. Currently not programmed in as of yet.
def withdrawl(balance, money):
print "Sorry, but this function is currently under construction!"
print
return
#This is an obvious one, this is where you check your balance.
def bank_balance(balance):
print "Balance: $", balance
return balance
# NOW THE PROGRAM REALLY STARTS, AS CODE IS RUN
balance = 0
balance = float(balance)
money = 5000
money = float(money)
loop = 1
choice = 0
while loop == 1:
choice = menu()
if choice == 1:
deposit = deposit(balance, money)
elif choice == 2:
withdraw = withdrawl(balance, money)
elif choice == 3:
balance = bank_balance(balance)
elif choice == 4:
loop = 0
print "Thank-You for stopping by the bank!"
#END OF THE PROGRAM Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.