HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Bank accounts - names, balances and passwords

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
passwordsbalancesnamesbankandaccounts

Problem

I'm learning python and I have written a bank program incorporating all the skills I've gathered so far. Using the program, one can create user profiles (name, password, account balance) which will be stored in three different text files. One can then do the normal bank transactions: deposit, withdraw, check balance or even close and delete their entire bank profile.
Every transaction is accompanied by a password check.

(There are two python files: Bank.py, filestore.py and three text files: cusbalfile.txt, cusnamefile.txt, cuspassfile.txt)

Bank.py

```
import filestore
import time
import datetime

#############################################################################
#This is the function that is called at the beginning of the program
def postbank(): ###
print ("Welcome to PostBank, We care for you\n") ###
prompt=int(raw_input("""To open a new bank account, Press 1\n"""+ ###
"""To access your existing account & transact press 2\n""")) ###
if prompt==1: ###
cus=BankAccount()#creates a new customer profile ###
elif prompt==2: ###
cus=ReturnCustomer()#checks for existing customer ###
else: ###
print "You have pressed the wrong key, please try again" ###
postbank() ###
###########################################################################################

##class for creating an instance of a new back account and other default bank functions
class BankAccount:

Solution

A few things to point out:

#############################################################################
#This is the function that is called at the beginning of the program


Please do not do this. Ever.

You don't need to wrap your initial code in a big, literal box.

A simple comment, or even multi-line comment like the following would be much better:

#This is the initial function, it does XYZ process.

"""This is the initial function, it does XYZ process.
   Usage:
       - sumOf(firstNumber, secondNumber)
       For example; the following:
       - sumOf(2, 3) should return 5
"""


Building on the last point, those """ are reserved for docstrings, which means you shouldn't be using them in functions, which means the following needs to go:

print ("Welcome to PostBank, We care for you\n")                                    ###
prompt=int(raw_input("""To open a new bank account, Press 1\n"""+                   ###                     
                    """To access your existing account & transact press 2\n"""))


Also, you should seperate the new-line characters into new strings and just concatenate them in. This makes for much clearer readability:

print("Welcome to PostBank, We care for you" + "\n")
prompt = int(raw_input("To open a new bank account, Press 1" + "\n"
                     + "To access your existing account & transact press 2:" + "\n"))


Additionally, you should move the opening message to a constant, that way, when/if you want to change/improve it, you can do it much more easily.

There's a few things to point out about this:

if prompt==1:                                                                       ###
    cus=BankAccount()#creates a new customer profile                                ###
elif prompt==2:                                                                     ###
    cus=ReturnCustomer()#checks for existing customer                               ###
else:                                                                               ###
    print "You have pressed the wrong key, please try again"                        ###
    postbank()


Instead of recursively calling the function, consider a while(true) loop instead.

while(true):
    prompt = int(raw_input("To open a new bank account, Press 1" + "\n"
                         + "To access your existing account & transact press 2:" + "\n"))
    if prompt==1:
        cus = BankAccount() #creates a new customer profile
    elif prompt==2:
        cus = ReturnCustomer() #checks for existing customer
    else:
        print "You have pressed the wrong key, please try again"


Let's improve that a bit further with use of a dictionary:

prompts = {
    1: BankAccount,   # Creates a new customer profile
    2: ReturnCustomer # Checks for existing customer
}
while(true):
    prompt = int(raw_input("To open a new bank account, Press 1" + "\n"
                         + "To access your existing account & transact press 2:" + "\n"))
    if prompt in prompts:
        prompts[prompt]()
    else:
        print "You have pressed the wrong key, please try again"


Note that cus wasn't assigned, because it does nothing.

class BankAccount:
    """Class for a bank account"""
    type="Normal Account"
    def __init__(self):
        ##calls functions in the module filestore
        self.username, self.userpassword, self.balance=filestore.cusaccountcheck()
        print ("Thank you %s, your account is set up and ready to use,\n a 100 pounds has been credited to your account" %self.username)
        time.sleep(2)
        self.userfunctions()


  • time.sleep(2): No. Remove it. (they're nearly useless, and certainly so in this context)



  • Use some whitespace between your operators. Read over Python's official style guide, PEP8 to see all the rules one must follow when writing correct Python.



  • instead of %s and %self.username use the "{username}".format(username=self.username) like format, it certainly improves readability, and removes the need for an explicit order.



  • Declare the variables on their own line: it's much better on readability to do so.



  • Your functions and variables should have undercases instead of nospace. See above point about PEP8



  • type goes nowhere, kill it. Useless/unused variables are redundant.



# Class for creating an instance of a new back account and other default bank functions
class BankAccount:
    def __init__(self):
        # Calls functions in the module filestore
        self.user_name
        self.user_password
        self.balance = file_store.cus_account_check()
        print ("Thank you {username}, your account is set up and ready to use,".format(username=self.username)
            + "\n" + "a 100 pounds has been credited to your account")
        self.user_functions()


Applying those changes globally, a refactored block would look like:

```
import filestore
import time
import datetime

WELCOME_MESSAGE = "Welco

Code Snippets

#############################################################################
#This is the function that is called at the beginning of the program
#This is the initial function, it does XYZ process.

"""This is the initial function, it does XYZ process.
   Usage:
       - sumOf(firstNumber, secondNumber)
       For example; the following:
       - sumOf(2, 3) should return 5
"""
print ("Welcome to PostBank, We care for you\n")                                    ###
prompt=int(raw_input("""To open a new bank account, Press 1\n"""+                   ###                     
                    """To access your existing account & transact press 2\n"""))
print("Welcome to PostBank, We care for you" + "\n")
prompt = int(raw_input("To open a new bank account, Press 1" + "\n"
                     + "To access your existing account & transact press 2:" + "\n"))
if prompt==1:                                                                       ###
    cus=BankAccount()#creates a new customer profile                                ###
elif prompt==2:                                                                     ###
    cus=ReturnCustomer()#checks for existing customer                               ###
else:                                                                               ###
    print "You have pressed the wrong key, please try again"                        ###
    postbank()

Context

StackExchange Code Review Q#103865, answer score: 8

Revisions (0)

No revisions yet.