AttributeError in my python code 2 - python

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()

Related

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

Inheritance and Printing in Bank account in python

This is a section from my bank account code, i am new to OOP and have added some inheritance to my code, the trouble i am having is with printing the balance with interest, when i print it I get the same value as the regular balance without interest, give your insight.
from random import randint
class BankAccount(object):
def __init__(self, initial_balance=0):
self.balance = initial_balance
def deposit(self, amount):
self.balance += amount
def withdraw(self, amount):
self.balance -= amount
def get_balance(self, initial_balance, rate):
return self.get_balance() * self._rate
class BankAccountWithInterest(BankAccount):
def __init__(self, initial_balance=0, rate=0.1):
BankAccount.__init__(self, initial_balance)
self._rate = rate
def interest(self):
return self.balance * self._rate
balance = (randint(100, 500))
my_account = BankAccount(balance)
my_interest = BankAccountWithInterest(balance)
print(my_account.balance)
print(my_interest.balance)
You have a couple of problems here, the main one being that you don't ever apply (call) the interest function, which is why you get the same balance for both. You should do:
print(my_account.balance)
print(my_interest.balance + my_interest.interest())
I would also recommend changing self._rate to rate in the get_balance method, otherwise you may get an error when you call get_balance and it tries to access self._rate. You should also have it return self.balance or self.balance + rate otherwise you are getting an infinite loop.
print(my_interest.balance + my_interest.interest())
Seems to return what you expect.
my_interest.balance = my_interest.balance + my_interest.interest()
print(my_interest.balance)
Will also update the balance with the added interest.

Python Call Parent Method from child method

I want to call the Method SavingsAccount.withdraw(600) but every time I get an exception TypeError: withdraw takes exactly 1 arguement(2 given). How do I fix this? Please advise.
class BankAccount(object):
def __init__(self):
pass
def withdraw(self):
pass
def deposit(self):
pass
class SavingsAccount(BankAccount):
def __init__(self):
self.balance = 500
def deposit(self, amount):
if (amount < 0):
return "Invalid deposit amount"
else:
self.balance += amount
return self.balance
def withdraw(self, amount):
if ((self.balance - amount) > 0) and ((self.balance - amount) < 500):
return "Cannot withdraw beyond the minimum account balance"
elif (self.balance - amount) < 0:
return "Cannot withdraw beyond the current account balance"
elif amount < 0:
return "Invalid withdraw amount"
else:
self.balance -= amount
return self.balance
class CurrentAccount(BankAccount):
def __init__(self, balance=0):
super(CurrentAccount, self).__init__()
def deposit(self, amount):
return super(CurrentAccount, self).deposit(amount)
def withdraw(self, amount):
return super(CurrentAccount, self).withdraw(amount)
x = CurrentAccount();
print x.withdraw(600)
The withdraw method in BankAccount is missing the amount:
class BankAccount(object):
def __init__(self):
pass
def withdraw(self): # <--- ADD THE AMOUNT HERE
pass
Same with the deposit method

having trouble with this python code

i get this error and i have tried going through the posts available on stack flow i do not still get the solution. this is the code
class BankAccount:
def _init_(self,balance):
self.balance = balance
def deposit(self,amount):
self.balance += amount
return self.balance
def withdraw(self,amount):
if amount>self.balance:
print "invalid transaction"
else:
self.balance -= amount
return self.balance
class MinimumBalanceAcccount(BankAccount):
// this is the error i get: IndentationError: expected an indented block
Its IndentationError.
complete the class definition.
class BankAccount:
def _init_(self,balance):
self.balance = balance
def deposit(self,amount):
self.balance += amount
return self.balance
def withdraw(self,amount):
if amount>self.balance:
print "invalid transaction"
else:
self.balance -= amount
return self.balance
class MinimumBalanceAcccount(BankAccount):
def __init__(self):
print "working"

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