5
\$\begingroup\$

I'm learning Python and have made this start of a ATM program, I'm very new to this so please be gentle..

Is there a better way to rewrite this?

CHECKING_BALANCE = 4000
SAVINGS_BALANCE = 1000

def welcome_screen():
    print """
    Welcome to the Bank of Bam!
    To continue please follow the instructions:
       Press 1 for Checking Account 
       Press 2 for Savings Account
       Press 3 for Balance Inquiries
       Press 4 to return card and exit
    """
    choice = int(raw_input("> "))
    if choice == 1:
        checking_account()
    elif choice == 2:
        savings_account()
    elif choice == 3:
        balance_inquiries()
    else:
        print "Exiting..."

def checking_account():
    print "You have $%i available" % (CHECKING_BALANCE)
    print """
    What would you like to do:
    [W]ithdrawl
    [D]eposit
    [T]ransfer funds
    """
    choice = raw_input("> ")
    if choice == "W":
        withdraw_funds()
    elif choice == "D":
        deposit_funds()
    elif choice == "T":
        transfer_funds
    else:
        print "Invalid input.."

def savings_account():
    print "You have $%i available" % (SAVINGS_BALANCE)
    print """
    [W]ithdrawl
    [D]eposit
    [T]ransfer
    """
    choice = raw_input("> ")
    if choice == "W":
        withdraw_funds()
    elif choice == "D":
        deposit_funds()
    elif choice == "T":
        transfer_funds()
    else:
        print "Invalid input.."

welcome_screen()

Example of usage:

    Welcome to the Bank of Bam!
    To continue please follow the instructions:
       Press 1 for Checking Account
       Press 2 for Savings Account
       Press 3 for Balance Inquiries
       Press 4 to return card and exit

> 1
You have $4000 available

    What would you like to do:
    [W]ithdrawl
    [D]eposit
    [T]ransfer funds

> Traceback (most recent call last): #<= CRTL - C
  File "atm.py", line 58, in <module>
    welcome_screen()


C:\Users\Justin\MyScripts>python atm.py

    Welcome to the Bank of Bam!
    To continue please follow the instructions:
       Press 1 for Checking Account
       Press 2 for Savings Account
       Press 3 for Balance Inquiries
       Press 4 to return card and exit

> 2
You have $1000 available

    [W]ithdrawl
    [D]eposit
    [T]ransfer

> Traceback (most recent call last): #<= CTRL - C
  File "atm.py", line 58, in <module>
    welcome_screen()
  File "atm.py", line 17, in welcome_screen
    savings_account()
  File "atm.py", line 48, in savings_account
    choice = raw_input("> ")
EOFError
\$\endgroup\$
  • \$\begingroup\$ Welcome to Code Review! Good job on your first answer. Perhaps you could improve your post by adding a sample session with a user using this program. \$\endgroup\$ – SirPython Feb 24 '16 at 20:20
  • \$\begingroup\$ @SirPython I can do that, give me a second \$\endgroup\$ – user98614 Feb 24 '16 at 20:21
  • \$\begingroup\$ @SirPython No they're CTRL - C tracebacks, I'm just looking for any better syntax in what I've written so far, this isn't completely finished (obviously) but I'm new and am very interested in learning \$\endgroup\$ – user98614 Feb 24 '16 at 20:29
1
\$\begingroup\$

The main thing I would say here is that you have 2 nigh identical functions apart from the balance being passed to them. Instead of checking_account and savings_account being different functions, make a single account_interaction function that takes an argument which you can then pass to the choden function.

def account_interaction(account):
    print "You have $%i available" % (account)
    print """
    What would you like to do:
    [W]ithdrawl
    [D]eposit
    [T]ransfer funds
    """
    choice = raw_input("> ")
    if choice == "W":
        withdraw_funds(account)
    elif choice == "D":
        deposit_funds(account)
    elif choice == "T":
        transfer_funds(account)
    else:
        print "Invalid input.."

Ideally account would be a class, or at least a dictionary that contains multiple relevant values (ie. type, amount, account holder), either of these makes it easier to pass them around functions like this.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.