3

There's a question I've been stuck on for days now: 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

Here's my solution:

class BankAccount(object):
 def __init__(self, name, balance = 90):
 self.name = name
 self.balance = balance
 def deposit(self, amount):
 self.balance += amount
 return self.balance
 def withdraw(self, amount):
 if self.balance >= amount:
 self.balance -= amount
 else:
 return 'invalid transaction'
class MinimumBalanceAccount(BankAccount):
 def __init__(self, name, minimum):
 self.name = name
 self.minimum = minimum

Here's the unittest that i had to work with:

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

But for some reason, when i tried submitting that result, i got back an error message that said my solution failed to pass all the tests. I'm at my wits end here, what am i doing wrong? Please help

Updated Information

Here is the error we are seeing:

Internal Error: runTests aborted: TestOutcomeEvent(handled=False, test=, result=, outcome='error', exc_info=(, TypeError('this constructor takes no arguments',), ), reason=None, expected=False, shortLabel=None, longLabel=None) is not JSON serializable

Michael Benjamin
376k110 gold badges617 silver badges739 bronze badges
asked Jan 6, 2016 at 13:23
4
  • It Seems code must be True, which assertion message you got back? Commented Jan 6, 2016 at 13:28
  • what are the results of the tests ? what happens ? Commented Jan 6, 2016 at 13:28
  • Do you mean that it failed to pass the tests written by whoever set the exercise? So it passes the tests you posted but not some other tests we can't see? That would be the spurious name parameter causing problems. Commented Jan 6, 2016 at 14:03
  • Hey Duncan, the unit test is the only test that was attached to the exercise. I removed the name parameter, now its just telling me an error occurred while running my script. It might be that there's a problem with the interpreter, at least I'm hoping that's the issue. Thanks to everybody for helping me check this. Commented Jan 6, 2016 at 18:53

4 Answers 4

3

You've accepted a name parameter in your class, which the unit test is not expecting or passing. Remove that.

answered Jan 6, 2016 at 13:31
Sign up to request clarification or add additional context in comments.

1 Comment

Technically the unit tests are passing the name parameter with a value of 90. This would become apparent if any of them used a non-default value for the balance.
2
class BankAccount(object):
def __init__(self, balance = 90):
self.balance = balance
def deposit(self, amount):
self.balance += amount
return self.balance
def withdraw(self, amount):
if self.balance >= amount:
 self.balance -= amount
else:
 return 'invalid transaction'
class MinimumBalanceAccount(BankAccount):
def __init__(self, minimum):
self.balance = minimum
answered Jan 11, 2016 at 9:05

2 Comments

A little explanation of your code wouldn't hurt anyone
I think it's self explanatory.
0

I've tested your code on my machine and the tests all pass without any problems.

Ran 5 tests in 0.001s
OK

One issue I spotted is that name argument in BankAccount constructor is useless and tests aren't doing anything with it.

answered Jan 6, 2016 at 13:36

Comments

0
class BankAccount(object):
def __init__(self,balance):
 self.balance = balance
def deposit(self, amount):
 self.balance += amount
def withdraw(self, amount):
 self.balance -= amount
 if amount > self.balance:
 return "invalid transaction"
 class MinimumBalanceAccount(BankAccount):
 pass
answered Jul 16, 2016 at 0:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.