Hello, everybody. I was wondering if anybody could help me with something. I don't know if I'm taking things too fast but, I was wondering if somebody could help with this program I am making. I'm currently writing a "fake" bank script. Basically you start with a set amount of money, and you can deposit it, withdraw it, and check your balance. However, when I make the deposit and then check my balance, I shows up as the balance being 0, instead of what I had deposited... Could somebody help me out?? Here's the code:

Bank.pyw:

``````#THIS PART IS NOT THE CODE ITSELF THESE ARE JUST COMMANDS
money = int(5000)
money = float(money)
#print the options you have
print "Welcome to the Python Bank System"
print " "
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:
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``````

So. If anybody can help with this, It'd be appreciated. Plus, I'll add you as one of the programmers and give credit where credit is due. Thanks, bye!

--Kippstah (Chris Moore)

## All 13 Replies

This code works around using different names for the same variable, and using the same name for a function and a variable. You still have problems with your logic though but hopefully if will get you started in the right direction.

``````#THIS PART IS NOT THE CODE ITSELF THESE ARE JUST COMMANDS
money = 5000.00
#print the options you have
print "Welcome to the Python Bank System"
print " "
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: ")

def deposit(balance, money):
""" Here is the deposit part.... This is where the user inputs
the amount of money they wish to deposit into their account.
"""
deposit = input("How much: \$")
deposit = float(deposit)

print "depositing balance = %7.2f,  money = %7.2f" % (balance, money)

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 balance

#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:
if choice == 1:
deposit_ret = deposit(balance, money)
balance = bank_balance(deposit_ret)
elif choice == 2:
withdraw = withdrawl(balance, money)
balance = bank_balance(withdraw)
elif choice == 3:
bank_balance(balance)
elif choice == 4:
loop = 0

print "Thank-You for stopping by the bank!"``````

This code works around using different names for the same variable, and using the same name for a function and a variable. You still have problems with your logic though but hopefully if will get you started in the right direction.

``````#THIS PART IS NOT THE CODE ITSELF THESE ARE JUST COMMANDS
money = 5000.00
#print the options you have
print "Welcome to the Python Bank System"
print " "
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: ")

def deposit(balance, money):
""" Here is the deposit part.... This is where the user inputs
the amount of money they wish to deposit into their account.
"""
deposit = input("How much: \$")
deposit = float(deposit)

print "depositing balance = %7.2f,  money = %7.2f" % (balance, money)

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 balance

#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:
if choice == 1:
deposit_ret = deposit(balance, money)
balance = bank_balance(deposit_ret)
elif choice == 2:
withdraw = withdrawl(balance, money)
balance = bank_balance(withdraw)
elif choice == 3:
bank_balance(balance)
elif choice == 4:
loop = 0

print "Thank-You for stopping by the bank!"``````

Thanks, it helped! It works perfectly... Also, I removed the + 1 for when you deposit so there wasn't an automatic 1 dollar in your acct. But, thank you for the help with this. :)

Hey... Uhh... Excuse me for double-posting but, uh... How do I get it so the money you start out with decreases as you deposit money, instead of staying at 1 constant value? Let me know, bye!

The function has to return money and balance. Otherwise, money is changed in the function's memory and then abandoned when you exit the function, i.e. the memory used by the function is garbage collected. Take a look at the explanation of local variables here http://en.wikibooks.org/wiki/Python_Programming/Functions

The function has to return money and balance. Otherwise, money is changed in the function's memory and then abandoned when you exit the function, i.e. the memory used by the function is garbage collected. Take a look at the explanation of local variables here http://en.wikibooks.org/wiki/Python_Programming/Functions

How do I return both, money and balance? I'm a little confused because I am new to programming... That's why I said in my original post that I hope I'm not taking things to fast.... ;)

Here is an example of a function returning two arguments ...

``````def get_name():
"""this function returns 2 arguments"""
first = "Fred"
last = "Ferkel"
return first, last

first, last = get_name()

print( first + " " + last )  # Fred Ferkel``````

Here is an example of a function returning two arguments ...

``````def get_name():
"""this function returns 2 arguments"""
first = "Fred"
last = "Ferkel"
return first, last

first, last = get_name()

print( first + " " + last )  # Fred Ferkel``````

Ok... So, I still can't figure this out. Can somebody rewrite the code from above, the bank code that is, and show me what exactly it should look like to make it so the money decreases?? I don't mean to be a pain.... lol... thanks!

No. Read the "We only give homework help to those who show effort" announcement http://www.daniweb.com/forums/announcement114-2.html Then take a look at one of the tutorials here http://wiki.python.org/moin/BeginnersGuide/NonProgrammers as this is the most basic of stuff that is in every tutorial and book.

Ok... So... In my functions for depositing and withdrawing, how come it doesn't return both when I type in "return balance, money"? Please let me know.... Because I've given it an effort, and tried everything I could think of, and have even tried looking at tutorials that show me the same exact thing you guys have posted, this is starting to be a pain... lol... Well. Let me know what your answers are, so I can figure this out. Thanks! Bye!

Hey Dude! I just solved your problem use

``````balance
``````

variable as a global variable and no need to pass it in the function because, it is a global variable so you change it's value from anywhere in the program just you to type

``````global balance
``````

or whatever the variable name is, use this keyword while you are changing the variable value in any of the function I am reposting your code with modifications please see it BYE.

``````#THIS PART IS NOT THE CODE ITSELF THESE ARE JUST COMMANDS
money = int(5000)
money = float(money)
#print the options you have
print "Welcome to the Python Bank System"
print " "
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(money):
global balance
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:
if choice == 1:
deposit = deposit(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
``````

how to code an exapmple of bank system by using classes which are stored in a file in python language.

@sam_38 Your question is way too vague. The best way to use a programming forum is to start your own thread and post code containing your attempts to solve the problem.

If your question is about persisting data on disk, there is a builtin module `pickle` that can persist almost arbitrary python objects. For a bank application one could also use a sqlite database for example, see the `sqlite3` module. For small volume of data, other persistence mechanisms are available, such as storing yaml files or json files.

guyz i typed this code for bubble sorting in python and i think it needs some alterations. could someone pls check it out for me. thanks a lot!!!!
i know there are some indentation errors.

``````def bubblesort( A ):
for i in range( len( A ) ):
for k in range( len( A ) - 1, i, -1 ):
if ( A[k] < A[k - 1] ):
swap( A, k, k - 1 )

def swap( A, x, y ):
tmp = A[x]
A[x] = A[y]
A[y] = tmp``````
commented: That's totally unrelated to this thread -2
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.