0

I am working on a project which I am spouse to create a Bank system using python. I have done the program and it works perfectly the only problem that I need help with is that how to create a registration form which will store user data for sign up, and read data for login from a txt file.

                                                          =
balance = 100

def log_in():
    tries = 1
    allowed = 5
    value = True
    while tries < 5:
        print('')
        pin = input('Please Enter You 4 Digit Pin: ')
        if pin == '1234':
            print('')
            print("             Your Pin have been accepted!          ")
            print('---------------------------------------------------')
            print('')
            return True
        if not len(pin) > 0:
            tries += 1
            print('Username cant be blank, you have,',(allowed - tries),'attempts left')
            print('')
            print('---------------------------------------------------')
            print('')
        else:
            tries += 1
            print('Invalid pin, you have',(allowed - tries),'attempts left')
            print('')
            print('---------------------------------------------------')
            print('')

    print("To many incorrect tries. Could not log in")
    ThankYou()




def menu():
        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")





def option1():
        print ('                     Deposit Money'      )
        print('')
        print("Your balance is  £ ",balance)
        Deposit=float(input("Please enter the deposit amount £ "))
        if Deposit>0:
            forewardbalance=(balance+Deposit)
            print("You've successfully deposited £", Deposit, "into your account.")
            print('Your avalible balance is £',forewardbalance)
            print('')
            print('---------------------------------------------------')
            service()

        else:
            print("No deposit was made")
            print('')
            print('---------------------------------------------------')
            service()




def option2():
    print ('                     Withdraw Money'      )
    print('')
    print("Your balance is  £ ",balance)
    Withdraw=float(input("Enter the amount you would like to Withdraw  £ "))
    if Withdraw>0:
        forewardbalance=(balance-Withdraw)
        print("You've successfully withdrawed £",Withdraw)
        print('Your avalible balance is £',forewardbalance)
    if Withdraw >= -100:
        print("yOU ARE ON OVER YOUR LIMITS !")
    else:
        print("None withdraw made")




def option3():
    print("Your balance is   £ ",balance)
    service()




def option4():
    ThankYou()


def steps():
    Option = int(input("Enter your option: "))
    print('')
    print('---------------------------------------------------')
    if Option==1:
        option1()
    if Option==2:
        option2()
    if Option==3:
        option3()
    if Option==4:
        option4()
    else:
        print('Please enter your option 1,2,3 or 4')
        steps()

def service():
    answer = input('Would you like to go to the menu? ')
    answercov = answer.lower()
    if answercov == 'yes' or answercov == 'y':
        menu()
        steps()
    else:
        ThankYou()

def ThankYou():
    print('Thank you for using Python Bank System v 1.0')
    quit()


log_in()
menu()
steps()

I expect my program to have a registration form which will store user data for sign up and read data for login from a .txt file.

Fouad
  • 3
  • 2
  • Aside from the fact that .txt is not suited to store data, with your current code it's complex. First, the last function ThankYou() will erase everything that was done before by closing the kernel (line quit()). Second, none of the function return any value / or use global values. Third, with a dataframe, you could have severals users, check if the pin correspond to a user, then register the activity on his account in the dataframe. – Mathieu Dec 03 '17 at 11:34

1 Answers1

0

So I looked a bit on it, and tried to do it with a .txt file.

So, in the same folder as the .py file, create the data.txt file like this one:

1234    1000.0
5642    500
3256    50.0
6543    25
2356    47.5
1235    495
1234    600000

The PIN and the balance are separated by a tabulation.

Then in the code, first step is to use global variable to pass on both the PIN number, the balance, and the path to the data file. Then, the data file is open, and the balance and pin are placed into 2 lists. If you want to work with a dataframe, for instance panda, a dictionnary structure would be more relevant.

# Create the global variable balance and pin that will be used by the function
global balance
global pin

# Path to the data.txt file, here in the same folder as the bank.py file.
global data_path
data_path = 'data.txt'
data = open("{}".format(data_path), "r")

# Create a list of the pin, and balance in the data file.
pin_list = list()
balance_list = list()
for line in data.readlines():
    try:
        val = line.strip('\t').strip('\n').split()
        pin_list.append(val[0])
        balance_list.append(val[1])
    except:
        pass

# Close the data file    
data.close()

""" Output
pin_list = ['1234', '5642', '3256', '6543', '2356', '1235', '1234']
balance_list = ['1000', '500', '-20', '25', '47.5', '495', '600000']
"""

Then, every function modifying the balance need to change the global variable value. For instance:

def log_in():
    global balance
    global pin
    tries = 1
    allowed = 5
    while tries < 5:
        pin = input('\nPlease Enter You 4 Digit Pin: ')
        if pin in pin_list:
            print('\n             Your Pin have been accepted!          ')
            print('---------------------------------------------------\n')
            balance = float(balance_list[pin_list.index(pin)])
            menu()
        else:
            tries += 1
            print('Wrong PIN, you have {} attempts left.\n'.format(allowed - tries))
            print('---------------------------------------------------\n')

    print('To many incorrect tries. Could not log in.')
    ThankYou()

Or:

def option1():
    global balance
    print ('                     Deposit Money\n')
    print('Your balance is  £ {}'.format(balance))
    deposit = float(input('Please enter the deposit amount £ '))
    if deposit > 0:
        balance = balance + deposit
        print("You've successfully deposited £ {} into your account.".format(deposit))
        print('Your avalible balance is £ {}\n'.format(balance))
        print('---------------------------------------------------')
        service()

    else:
        print('No deposit was made. \n')
        print('---------------------------------------------------')
        service()

Others sources of improvement:

  • Instead of print ('') to skip a line, simply add '\n' to the printed string.
  • Use 'blabla {}...'.format(value to put in {}) to place values, or str, or whatever in the middle of a str. It's good to use it since you will be able to place multiple values in the same string. Illustration in the saving data part bellow.

Finally, the new balance must be save in the .txt file. This will be done in the Thank you function:

def ThankYou():
    global balance
    global pin
    global data_path
    balance_list[pin_list.index(pin)] = balance

    data = open("{}".format(data_path), "w")
    for i in range(len(pin_list)):
        line = '{}\t{}\n'.format(str(pin_list[i]), str(balance_list[i]))
        data.write(line)
    data.close()

    print('Thank you for using Python Bank System v 1.0')
    quit()

I hope you get the idea, and can manage on your own the code modifications, you should have every key to do it.

Good luck !

Mathieu
  • 5,410
  • 6
  • 28
  • 55
  • That helped a lot, thank you, but how can I make new users register with a special PIN and then write it onto the .txt file so they can use it in the future? – Fouad Dec 03 '17 at 16:35
  • Add an option new PIN somewhere in your function, and simply append the new PIN and new Balance (users inputs) to the list pin_list and balance_list before you execute the function Thank you that will overwrite the data.txt file. – Mathieu Dec 03 '17 at 16:41