PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, November 24, 2022

[FIXED] How to do I connect my two modules within my Bank and Account classes?

 November 24, 2022     class, function, module, python     No comments   

Issue

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)

Solution

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)


Answered By - James
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing