コード例 #1
0
def log_in(event):
    '''Function to log in to the banking system using a known account number and PIN.'''
    global account
    global pin_number_var
    global account_number_var

    read = event + '.txt'
    # Create the filename from the entered account number with '.txt' on the end
    # Try to open the account file for reading
    accept_loging = False
    try:
        with open(str(read),
                  'r') as account_file:  # Open the account file for reading

            account_number = account_file.readline(
            )[:-1]  # First line is account number

            account_pin = account_file.readline(
            )[:
              -1]  # Second line is PIN number, raise exceptionk if the PIN entered doesn't match account PIN read

            if account_pin != pin_number_var.get():
                raise ValueError("Pin doesnt match")
            else:
                accept_loging = True

            account_balance = float(account_file.readline(
            )[:-1])  # Read third and fourth lines (balance and interest rate)

            account_interest = float(account_file.readline()[:-1])
            transaction_list = []
            # Section to read account transactions from file - start an infinite 'do-while' loop here

            # Attempt to read a line from the account file, break if we've hit the end of the file. If we
            # read a line then it's the transaction type, so read the next line which will be the transaction amount.
            # and then create a tuple from both lines and add it to the account's transaction_list
            while True:
                _type = account_file.readline()[:-1]  # Attempt to read a line
                if not _type:  # If we failed, then exit
                    break
                _amount = float(account_file.readline()[:-1])

                transaction_list.append((_type.strip(), _amount))

            account.account_number = account_number
            account.account_pin = account_pin
            account.balance = account_balance
            account.account_interest = account_interest
            account.transaction_list = transaction_list
        # Close the file now we're finished with it (context manager is doing this for us)
    except ValueError:
        messagebox.showerror("Error", "Incorrect PIN")
        account = BankAccount()
        account.account_interest = .30
        account = account_number_var
        clear_pin_entry("event")
        account_number_entry.focus_force()
    except Exception:
        # Catch exception if we couldn't open the file or PIN entered did not match account PIN
        messagebox.showerror("Error", "file doesnt exists")
        account = BankAccount()
        clear_pin_entry("event")
        account_number_entry.focus_force()

        # Show error messagebox and & reset BankAccount object to default...

        #  ...also clear PIN entry and change focus to account number entry
    if accept_loging:
        remove_all_widgets()
        create_account_screen()