Nosetests error in code where exception is manually raised - python

I wrote a simple program to practice but its test code keeps on failing. Here is the main code:
class Bank(object):
def __init__(self, name, deposit=0, withdraw=0, balance=0.0):
self.name = name
self.deposit = deposit
self.withdraw = withdraw
self.balance = balance
def deposited(self):
self.balance += self.deposit
return self.balance
def withdrawn(self):
if self.balance < self.withdraw:
raise ValueError("INSUFFICIENT BALANCE!")
else:
self.balance -= self.withdraw
return self.balance
This is its test code:
from nose.tools import *
from bank.account import Bank
def test_account():
Maria = Bank('Maria', None, None)
assert_equal(Maria.name, 'Maria')
` `assert_equal(Maria.balance, 0.0)
assert_equal(Maria.deposit, None)
assert_equal(Maria.withdraw, None)
Sam = Bank('Sam', 10, 1)
assert_equal(Sam.name, 'Sam')
assert_equal(Sam.balance, 0.0)
assert_equal(Sam.deposit, 10)
assert_equal(Sam.withdraw, 1)
def test_deposit():
Rotschild = Bank('Rotschild', 100, 0)
Rotschild.deposited()
assert_equal(Rotschild.balance, 100)
def test_withdaw():
Morgan = Bank('Morgan', 100, 50)
Morgan.deposited()
Morgan.withdrawn()
assert_equal(Morgan.balance, 50)
Bill = Bank('Bill', 5, 10)
Bill.deposited()
assert_raises(ValueError, Bill.withdrawn())
Upon running nosetests, it always shows Failed(error=1), i cannot figure out if it is a problem with the syntax or what.
I want the test to raise the exception when the account balance is less than the amount withdrawn.

Related

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

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.

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

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

Categories

Resources