Multi level inheritance python - 'CheckingAccount' object has no attribute 'balance' - python

I am working on a python project. All was working well when I just had one level of inheritance but once I added SavingAccount and CheckingAccount which should be a child of BankAccount which is a child of Customer I started to get the following error: 'CheckingAccount' object has no attribute 'balance'
I assumed I would do the second layer of inheritance the same as the first but maybe I must be missing. Thanks in advance for any help!
class Customer:
def __init__(self,firstName, lastName, social):
self.firstName = firstName
self.lastName = lastName
self.social = social
def setfirstName(self,firstName):
self.firstName = firstName
def setlastName(self,lastName):
self.lastName = lastName
def __str__(self):
self.name = "{},{} (SSN:{})".format(self.firstName, self.lastName,self.social)
return self.name
class BankAccount(Customer):
from random import randint
n = 10
range_start = 10**(n-1)
range_end = (10**n)-1
accountNumber = randint(range_start, range_end)
def __init__(self,customer,balance = 0):
self.customer = customer
self.balance = balance
def setCustomer(self,customer,accountNumber):
self.customer = customer
self.accountNumber = accountNumber
def getCustomer(self,customer,accountNumber):
return self.customer, self.accountNumber
def deposit(self, amount):
self.balance = self.balance + amount
return self.balance
def withdrawal(self, amount):
self.balance = self.balance - amount
return self.balance
def __str__(self):
customer = "{} account number: {}, balance: ${}".format(self.customer,self.accountNumber,self.balance)
return customer
class CheckingAccount(BankAccount):
def __init__(self, bankAccount):
self.bankAccount = bankAccount
def applyAnnualInterest(self):
excess = self.balance - 10000
if excess > 0:
interest = (excess * .02)
self.balance = self.balance + interest
return self.balance
else:
return self.balance
class SavingAccount(BankAccount):
def __init__(self, bankAccount):
self.bankAccount = bankAccount
def applyAnnualInterest(self):
interest = (self.balance * .05)
self.balance = self.balance + interest
return self.balance
def main():
alin = Customer('Alin', 'Smith', '111-11-1111')
mary = Customer('Mary', 'Lee', '222-22-2222')
alinAccnt = CheckingAccount(alin)
maryAccnt = SavingAccount(mary)
alinAccnt.deposit(20000)
print(alinAccnt)

You need to initialise the parent; try:
class CheckingAccount(BankAccount):
def __init__(self, bankAccount):
super(CheckingAccount, self).__init__()
self.bankAccount = bankAccount
Don't forget the intermediate class too!
class BankAccount(Customer):
def __init__(self,customer,balance = 0):
super(BankAccount, self).__init__()
self.customer = customer
self.balance = balance
This will ensure the parent constructors get called too.

Related

NoneType object has no attribute

def forecast(bank, years):
class Bank:
def __init__(self, name):
self.name = name
self.mark_cap = 0
self.acc_list = []
self.age = 0
def lend(self, principal, ann_inc):
self.mark_cap -= principal
def forward_year(self):
self.age += 1
def back_year(self):
if self.age == 0:
self.age = 0
self.age -= 1
def show_high(self):
print(Bank.acc_list[0])
class Account:
def __init__(self, ID, password):
self.ID = ID
self.password = password
if len(password) < 5:
print('Password must be at least 5 characters')
self.amount = 0
self.interest = 0.0175
self.acc_org = [ID, password, self.amount, self.interest]
def deposit(self, x):
self.amount += x
self.acc_org[2] = self.amount
def withdraw(self, y):
self.amount -= y
self.acc_org[2] = self.amount
def threshold(self):
if self.amount >= 1000000:
self.interest = 0.02
def comp_int(self, n):
self.threshold()
self.amount *= (1 + self.interest)**n
self.acc_org[2] = self.amount
def show_amount(self):
print(self.amount)
def add_2_bank(self, name):
bank_name = name
bank_name.acc_list.append(self.acc_org)
X = Bank('Bank of china')
Account1 = Account('12345', '12345')
Account1.deposit(200)
Account1.comp_int(2)
Account1.add_2_bank(X)
X.show_high()
The error that I am getting is that my 'Bank' object (X) has no attribute acc_list(). Someone please help me.
In the show_high method, modify Bank.acc_list to self.acc_list. Only static properties can be used like Bank.*.

How to force the value on init?

I have defined following class of a bank account. The account should always start with 0.0 balance. How can I enforce that the value is always set 0.0 even if the user set it differently at the initiation?
class Account(object):
def __init__(self, name, balance=0.0):
self.name = name
self.balance = balance
def add_money(self, deposit_amnt):
self.balance += deposit_amnt
def withdraw_money(self, withdraw_amnt):
if withdraw_amnt > self.balance:
raise ValueError('Withdraw amount is more than balance')
else:
self.balance -= withdraw_amnt
def check_balance(self):
return self.balance
my_account = Account('Tim', 15)
my_account.check_balance()
>>> 15
You can ommit balance in __init__ if you want to start with 0. You have add method to do that later.
def __init__(self, name):
self.name = name
self.balance = 0

Simple class Property Update

I have a class where I'm expecting this:
print(rithesh.amount) = 150.
How can I do this?
Here is my code:
class Customer:
total_amount = 0
def __init__(self, name, mob, email, amount=None):
self.name = name
self.mob = mob
self.eamil = email
def add_amount(self, amount):
self.amount = amount
rithesh = Customer("Rithesh", "8896398598", "ritheshb1#gmail.com")
rithesh.add_amount(100)
rithesh.add_amount(50)
print(rithesh.amount)
You can declare your amount variable in your __init__ method as 0. Then make a small change in your add_amount method.
class Customer:
total_amount = 0
def __init__(self, name, mob, email, amount=None):
self.name = name
self.mob = mob
self.eamil = email
self.amount = 0
def add_amount(self, amount):
self.amount += amount
rithesh = Customer("Rithesh", "8896398598", "ritheshb1#gmail.com")
rithesh.add_amount(100)
rithesh.add_amount(50)
print(rithesh.amount)
output
150
The actual way of having properties in python is by using #property decorator
for example, in your class:
class Customer:
total_amount = 0
def __init__(self, name, mob, email, amount=None):
self.name = name
self.mob = mob
self.eamil = email
#property
def add_amount(self):
return self.add_amount
#add_amount.setter
def add_amount(self, amount):
self.add_amount = amount
rithesh = Customer("Rithesh", "8896398598", "ritheshb1#gmail.com")
rithesh.add_amount = 150
print(rithesh.add_amount)
Got how to do it.
i have to declare the value self.amount = 0 during initialization.
class Customer:
total_amount = 0
def __init__(self, name, mob, email, amount=None):
self.name = name
self.mob = mob
self.eamil = email
self.amount = 0
def add_amount(self, amount):
self.amount += amount
rithesh = Customer("Ritehsh", "8892398598", "ritheshb1#gmail.com")
rithesh.add_amount(100)
rithesh.add_amount(50)
print(rithesh.amount)
hence getting output as print(rithesh.amount) = 150
What is happening is that when you call add_amount you are not adding the value to self.amount you are just setting it.
Just change the definition of add_amount from:
self.amount = amount
to:
self.amount += amount
And add to the __init__ method:
self.amount = 0

AttributeError in my python code 2

I have the following line of codes in python:
class BankAccount:
def __init__ (self,balance = 0):
self.balance = balance
def deposit(self, deposit_amount= 30):
self.deposit_amount=deposit_amount
balance += deposit_amount
return balance
def withdraw (self,withdraw_amount= 10):
if withdraw_amount > balance:
raise RuntimeError('Invalid Transaction')
balance -= withdraw_amount
return balance
class MinimumBalanceAccount(BankAccount):
def __init__(self,balance = 0):
self.balance = balance
c = BankAccount()
c.deposit(50)
it gives me this error:
AttributeError("BankAccount instance has no attribute 'deposit'"
If your indentation is actually as you posted it, then the deposit function is defined within the __init__ method. As such, it's not an attribute to the class, but a mere function available within the __init__ method. The following code is simply a copy of your code, but with indentation fixed:
class BankAccount:
def __init__(self,balance=0):
self.balance = balance
def deposit(self, deposit_amount=30):
self.deposit_amount=deposit_amount
self.balance += deposit_amount
return self.balance
def withdraw(self,withdraw_amount=10):
if withdraw_amount > self.balance:
raise RuntimeError('Invalid Transaction')
self.balance -= withdraw_amount
return self.balance
class MinimumBalanceAccount(BankAccount):
def __init__(self,balance = 0):
self.balance = balance
c = BankAccount()
c.deposit(50)
This code works for me, as it does when I replace c = BankAccount() with c = MinimumBalanceAccount()

Why does this class program not return the balance?

Question: Develop a class BankAccount that supports these methods:
__init__(): Initializes the bank account balance to the value of the input argument or to 0 if no input argument is given
withdraw(): Take an argument as an input and withdraws it from the balance
deposit(): Take an amount as an input and add it to the balance
balance(): Returns the balance on the account
class ValueErrorException (Exception):
pass
class BankAccount:
accounts = 0
def __init__ (self, bal = 0.0):
BankAccount.accounts += 1
self.accountNumber = str(BankAccount.accounts)
self.balance = bal
def withdraw(self, amount):
if self.balance - amount < 0:
raise ValueErrorException("Illegal balance")
else:
self.balance -= amount
def deposit (self, amount):
self.balance += amount
def balance(self, amount):
return amount
The balance definition should be like this:
def balance(self):
return self.balance
You may also want to consider changing the variable name from balance to accountBalance so it doesn't affect the definition that is named the same. Your new code would now be:
class ValueErrorException (Exception):
pass
class BankAccount:
accounts = 0
def __init__ (self, bal = 0.0):
BankAccount.accounts += 1
self.accountNumber = str(BankAccount.accounts)
self.accountBalance = bal
def withdraw(self, amount):
if self.accountBalance - amount < 0:
raise ValueErrorException("Illegal balance")
else:
self.accountBalance -= amount
def deposit (self, amount):
self.accountBalance += amount
def balance(self):
return self.accountBalance
return self.balance
access the classes instance variable, not a functional argument. no need to pass amount to a function just to return it

Categories

Resources