#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0

    Exclamation ATM Program, how to update balance value?


    Hi everyone, currently writing a program for an ATM machine. It's for college work.

    At the moment I have a Menu system in place.

    The menu looks like this:
    (0)End Program
    (1) Show Balance
    (2) Cash Withdraw

    very basic!

    My problem or question is, let's say I select Balance. It will say ''Balance is $600'' or what ever. Then if I choose Cash Withdraw, I have it set up so it will minus the input (that the user selects) from the balance. It will then display my new balance.

    So for example...

    I select Balance, it tells me $600.
    If I then choose Cash Withdraw and enter $100, it will then print ''Your new balance is $500"

    But then, because of my loop, it will then display the menu again, and if i then choose ''Show Balance'', it will say ''Your balance is ''$600''

    It tells me this because at the start of my code, I have ''balance=600"

    I'm not sure how to ''updat'' my balance after I do the withdraw. I have to return the value somewhere.

    Any help is much appreciated.

    My code:


    def displayMenu():
    print("(0) End Program")
    print("(1) Display Balance")
    print("(2) Withdraw Cash")
    return

    def select(choice):

    if choice == "1":
    BE()
    elif choice == "2":
    CW()

    return

    #This is what the menu does and the choice does
    def getChoice():

    choice=input("Enter choice Number: ")
    while (choice != "0") and (choice != "1") and (choice != "2"):
    choice=input("Enter choice Number: ")

    if choice == "0":
    print("Ending Program - Thank You!")

    return choice


    #These are the blocks of code
    def BE():

    print ("Thank you. Your current balance is: ", balance)
    return


    def CW():

    amount=float(input("Please enter the amount you would like to withdraw: "))
    newbal=balance-amount
    print ("Thank you! Your new balance is: ", newbal)
    return

    # Program starts here

    balance=600

    while True:
    displayMenu()
    c = getChoice()
    if (c=="0"):
    break
    select(c)
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481
    Eventually you may learn to use classes thereby avoiding many global variables. For now,
    Code:
    def CW():
        global balance
        amount=float(input("Please enter the amount you would like to withdraw: "))
        balance -= amount
        print ("Thank you! Your new balance is: ", balance)
    Also, looks like you're coming from some other programming language.

    If a function doesn't terminate with a return statement python returns None . You can safely remove the empty "return" statements from most of your functions. python doesn't require parentheses around condition expressions in either the while or if statements.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    4
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Eventually you may learn to use classes thereby avoiding many global variables. For now,
    Code:
    def CW():
        global balance
        amount=float(input("Please enter the amount you would like to withdraw: "))
        balance -= amount
        print ("Thank you! Your new balance is: ", balance)
    Also, looks like you're coming from some other programming language.

    If a function doesn't terminate with a return statement python returns None . You can safely remove the empty "return" statements from most of your functions. python doesn't require parentheses around condition expressions in either the while or if statements.
    Ah, now I understand that it's global. Yeah I'm still in the early stages of Python but getting wiser by the day. I find that it's very different from other languages such as Java.

    But thank you very very very much! Your help and advice really made the difference. Thank you
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    19
    Rep Power
    0
    Originally Posted by b49P23TIvg
    Eventually you may learn to use classes thereby avoiding many global variables. For now,
    Code:
    def CW():
        global balance
        amount=float(input("Please enter the amount you would like to withdraw: "))
        balance -= amount
        print ("Thank you! Your new balance is: ", balance)
    Man I had my nuts chopped off a few times getting caught advising someone to use global, but it was only cos I knew the other person was new to scopes and functions and I pointed it out clearly that's it's a temporary fix. Glad to see someone more experienced shares my point of reason.

IMN logo majestic logo threadwatch logo seochat tools logo