Home > Mobile >  How to do I connect my two modules within my Bank and Account classes?
How to do I connect my two modules within my Bank and Account classes?

Time:10-25

I'm a newbie to python. I'm writing a banking program script. The Bank has a defined name and address. Where the Account has a defined, first name, last name, and balance.

I'm testing the code by trying to transfer money from one account to another. But I'm getting an error after entering accounts. Where did I go wrong?

from account import Account


class Bank:
    name = ''
    address = ''
    all_accounts = {}

    def __init__(self, name, address):
        self.name = name
        self.name = address

    def create_account(self, firstname, lastname, balance):
        accounts = Account(firstname, lastname, balance)
        self.all_accounts[Account] = accounts

    def show_accounts(self, show_history=False):
        for account in self.all_accounts.items():
            print(account)


    def transfer(self, ac1, ac2, balance):
        if self.all_accounts[ac1].withdraw(balance):
            self.all_accounts[ac2].deposit(balance)

    def get_account(self, account_customers):
        return self.all_accounts[account_customers]
class Account:
    # firstname = ''
    # lastname = ''
    # balance = ''

    def __init__(self, firstname, lastname, balance=0):
        self.firstname = firstname
        self.lastname = lastname
        self.balance = balance

        self.number_of_deposits = 0
        self.number_of_withdraws = 0

        self.history = []

    def desc(self):
        print("Name is: ", self.firstname, self.lastname)
        print("Balance: ", self.balance)
        return True

    def deposit(self, value):
        self.balance  = value
        self.history.append(value)
        self.number_of_deposits  = 1
        return True

    def withdraw(self, value):
        if self.balance < value:
            print(f'{self.firstname, self.lastname} not enough money')
            return False

        self.balance -= value
        self.history.append(-value)
        self.number_of_withdraws  = 1
        return True

    def transfer(self):
        answer = int(input("Enter amount of $ to transfer"))
        if answer < 0:
            print("Choose a value above $0")
        else:
            print("Your transfer was successful")
from account import Account
from bank import Bank

bank = Bank("Bank of Westeros", "1 Park Place, Westeros, GoT")

ac1 = bank.create_account("Carrot", "Top", 5000)
ac2 = bank.create_account("Dolly", "Parton", 10000)

CodePudding user response:

To add an item to a list, use the .append method. What you have written in the last line of create_account doesn't make sense. Replace:

self.all_accounts[Account] = accounts

with this:

self.all_accounts.append(accounts)
  • Related