BankAccount Class with Error........Help!


#1

Please I need help ASAP on this:
I've been trying and battling to run this code on python but it keeps coming up with errors and I've looked through the code over and over again but I just can't seem to find where the error is from. please I would be glad if anyone can help me out.

This is the question/test:

Create a class called BankAccount
Create a constructor that takes in an integer and assigns this to a balance property.
Create a method called deposit that takes in cash deposit amount and updates the balance accordingly.
Create a method called withdraw that takes in cash withdrawal amount and updates the balance accordingly. if amount is greater than balance return "invalid transaction"
Create a subclass MinimumBalanceAccount of the BankAccount class

import unittest
class AccountBalanceTestCases(unittest.TestCase):
def setUp(self):
self.my_account = BankAccount(90)

def test_balance(self):
self.assertEqual(self.my_account.balance, 90, msg='Account Balance Invalid')

def test_deposit(self):
self.my_account.deposit(90)
self.assertEqual(self.my_account.balance, 180, msg='Deposit method inaccurate')

def test_withdraw(self):
self.my_account.withdraw(40)
self.assertEqual(self.my_account.balance, 50, msg='Withdraw method inaccurate')

def test_invalid_operation(self):
self.assertEqual(self.my_account.withdraw(1000), "invalid transaction", msg='Invalid transaction')

def test_sub_class(self):
self.assertTrue(issubclass(MinimumBalanceAccount, BankAccount), msg='No true subclass of BankAccount')

And this is the error that keep on popping out when I run the test

Traceback (most recent call last):
File "python/nose2/bin/nose2", line 8, in
discover()
File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/main.py", line 300, in discover
return main(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/main.py", line 100, in init
super(PluggableTestProgram, self).init(**kw)
File "/usr/local/lib/python2.7/dist-packages/unittest2/main.py", line 89, in init
self.parseArgs(argv)
File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/main.py", line 133, in parseArgs
self.createTests()
File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/main.py", line 258, in createTests
self.testNames, self.module)
File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/loader.py", line 67, in loadTestsFromNames
for name in event.names]
File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/loader.py", line 82, in loadTestsFromName
result = self.session.hooks.loadTestsFromName(event)
File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/events.py", line 224, in call
result = getattr(plugin, self.method)(event)
File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/plugins/loader/testclasses.py", line 119, in loadTestsFromName
result = util.test_from_name(name, module)
File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/util.py", line 106, in test_from_name
parent, obj = object_from_name(name, module)
File "/usr/local/lib/python2.7/dist-packages/nose2-0.5.0-py2.7.egg/nose2/util.py", line 117, in object_from_name
module = import('.'.join(parts_copy))
File "/home/ubuntu/Applications/andelabs-server/tmp/56cb357579dc061400ac8783-565b07b72568f41500779d20-test.py", line 4, in
from tmp.andelabs_56cb357579dc061400ac8783_565b07b72568f41500779d20 import *
File "/home/ubuntu/Applications/andelabs-server/tmp/andelabs_56cb357579dc061400ac8783_565b07b72568f41500779d20.py", line 1
class BankAccount:
^
IndentationError: unexpected indent

And this is my own code I've been trying to run

class BankAccount:
    def __init__(self, balance):
      self.balance = balance
      return balance
      
    def deposit(self, new_Amount):
      self.new_Amount = new_Amount
     self.balance + new_Amount
     self.__checkinput(new_Amount)
      if (new_Amount <= self.balance):
        raise Exception("Invalid Transaction")
      else (new_amount > self.balance):
        return self.balance
        
    def withdraw(self, withdrn_Amnt):
      self. withdrn_Amnt =  withdrn_Amnt
      self.balance - withdrn_Amnt
      if (withdrn_Amnt >= self.balance)
      raise Exception("Invalid Transaction")
      else (withdrn_Amnt < self.balance):
        return self.balance
        
    def __checkinput(self, value):
	   if value < 0:
		 return ("Enter a valid amount")
      
   class MinimumBalanceAccount(BankAccount):
   	def __init__(self,MinBalance
   	self.Minbalance = Minbalance ):  


acct = BankAccount(90)
acct.deposit(90)
acct.withdraw(40)
acct.withdraw(1000)


	unittest.main()


#2

Hi
I am not sure if I can help you through your problem. According to the error message that you get IndentationError: unexpected indent, there is (are) some indentation(s) that are incorrect in the code.

1) In def deposit(self, new_Amount): the second and third lines

self.balance + new_Amount
self.__checkinput(new_Amount)

are not aligned with the first one.

2) In def withdraw(self, withdrn_Amnt): there is a space between self and withdrn_Amnt in the first line self. withdrn_Amnt = withdrn_Amnt. Moreover raise Exception("Invalid Transaction") should be indented at the same level as in the else below

3) the line class MinimumBalanceAccount(BankAccount): should not have any indentation

4) the constructor init in the class MinimumBalanceAccount is not complete or you did not write the code in your message

5) the last line unittest.main() should not be indented


#3

thank you very much @thebelgian1 I was able to use what you pointed out as the error to correct the code and I have been to run it properly but it now gives this new error.Please if you can help out. Thank you.

File
acct = BankAccount(90)
TypeError: init() should return None

and this is the corrected code:

class BankAccount:
 def __init__ (self, balance):
 self.balance = balance
 return balance

def deposit(self, new_Amount):
 self.new_Amount = new_Amount
 self.balance += new_Amount
 self.__checkinput(new_Amount)
 return self.balance
    
def withdraw(self, withdrn_Amnt):
  self.withdrn_Amnt = withdrn_Amnt
  self.balance - withdrn_Amnt
  if(withdrn_Amnt > self.balance):
    raise Exception("Invalid Transaction")
  else:
    withdrn_Amnt < self.balance
    return self.balance
    
def __checkinput(self, value):
   if value < 0:
	 return ("Enter a valid amount")

class MinimumBalanceAccount(BankAccount):
def init(self,MinBalance = 10):
self.Minbalance = MinBalance

acct = BankAccount(90)
acct.deposit(90)
acct.withdraw(40)
acct.withdraw(1000)

unittest.main()


#4

Hi
In your constructor __init__, there is a return balance. This is forbidden inside a constructor. The constructor is used to initiate values in the class, not to return them. Moreover, the line self.balance = balance should be indented with respect to def __init__ (self, balance):
Cheerio


#5

thank you very much for all your help, it was very useful and finally I was able to come up with the solution. I really appreciate it.


#6

I was practicing with this code but when i got to the last line i.e.
unittest.main()
The error code thrown back at me was "NameError: name unittest not defined.
I would appreciate if anybody can solve this for me. How do i define name "unittest".


#7

@walukoe first of all you have to know whether the code you're working with is a unittest based code. If its not then its easier but if it is then, why it keeps on bringing the error: name unittest is not defined is because either you have not specified that it might be a unittest somewhere.

1st a reason might be that at the end of your code you have not specified that:

if__name__=='main':
unittest.main(verbosity=2)

this simply tells the compiler that it should compile your code in relation to the unittest test instance given.

Secondly, it might be because the test given in the first has not been added to the code. If it is a unittest code most times you must import your unittest code by 1st of all:

ADDING
import unittest

AND ADDING YOUR TEST CODE INSTANCE NEXT: example is like this code instance for this particular code:

class AlgorithmTestCases(unittest.TestCase):
def test_maximum_number_one(self):
.........................................
.....................etc..

by this you tell the compiler that okay 1st of all this is a unittest code and it has a test instance that has to be passed in order for the running of the code to be successful then putting it all together the 1st set of codes after the "import unittest" is the test instance given to you by the supposed people that set the test and the next set of codes should be yours before you write finally the "if__name__=='main':
unittest.main(verbosity=2)"

this way the compiler runs your answer code with relation to the test instance to see if your code has successfully passed the defined instances its supposed to.

I know have written a lot of bants but its simply to break it all down in order for you to understand and i hope this can be of great help btw.


#8

i would like to be assisted in this class


#9

Please do not revive old topics. If you need help, please create a new topic,

Thanks.


#10

@adenrele i would like to have the 'unittest' word with you.