'How to import login/registration database in second part of code?

I have written 2 pieces of code.

  1. login and authentication
 def register():
    db = open("database.txt","r")
    account_number= input("Enter your Account Number: ")
    pin = input("Create a 4-digit pin: ")
    pin1 = input("Confirm Pin: ")
    d = []
    f = []
    for i in db:
        a,b = i.split(", ")
        b = b.strip()
        d.append(a)
        f.append(b)
    data = dict(zip(d,f))
    print(data)

    if pin != pin1:
        print("Pins don't match, try again!")
        register()
    else:
        if len(pin) != 4:
            print("Pin is not 4-digit. Pin must be 4-digit")
            register()
        elif account_number in d:
            print("account number already exists")
            register()
        else:
            db = open("database.txt","a")
            db.write(account_number+", "+pin+", 0" "\n")
            print("Success!")
    db.close()


def access():
    db = open("database.txt", "r")
    account_number = input("Enter your account number: ")
    pin = input("Enter your pin: ")

    if not len(account_number or pin)<1:
        d = []
        f = []
        for i in db:
            a, b = i.split(", ")
            b = b.strip()
            d.append(a)
            f.append(b)
        data = dict(zip(d, f))

        try:
            if data[account_number]:
                try:
                    if pin== data[account_number]:
                        print("Login Success")
                    else:
                        print("Pin or Account Number Incorrect")
                except:
                    print("Pin or Account Number Incorrect")
            else:
                print("Account Number doesn't exist")
        except:
            print("Login error")
    else:
        print("Please Enter your login credentials")
    db.close()
def home(option = None):
    option = input("Login | Signup: ")
    if option == "Login":
        access()
    elif option == "Signup":
        register()
    else:
        print("Please choose an option")
home()
  1. money transactions
choice = 0
while choice != 4:
      print("\n\n**** Menu *****")
      print("1 -- balance")
      print("2 == deposit")
      print("3 == withdraw")
      print("4 == cancel\n")

      choice=int(input("\nEnter your option:\n"))
      if choice==1:
             print("Balance = ", +balance)
      elif choice==2:
           dep=int(input("Enter your deposit: "))
           balance+=dep
           print("\n deposited amount: " , +dep)
           print("balance = ", +balance)
      elif choice==3:
           wit=int(input("Enter the amount to withdraw: "))
           balance -= wit
           print("\n withdrawn amount: " , +wit)
           print("balance =", +balance)
      elif choice ==4:
           print('Session ended,goodbye.')
      else:
           print("Invalid Entry")

The first code stores an account number and pin in the database. The second code should use the balance from the database and perform the transactions. After performing the transactions it should update the database. How can I import the database made in the first code to be used in the second code?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source