2016-04-04 3 views
-1
class BankAccount: 
    def __init__(self, deposit, withdraw, balance): 
    self.balance = balance 
    self.deposit = deposit 
    self.withdraw = withdraw 

    def balance(self): 
     self.balance = balance 

     def withdraw(self, amount): 
     self.balance -= amount 
     print 'self.balance' 
     def deposit(self, amount): 
      self.balance += amount 
      print 'self.balance' 

a = BankAccount(90,40,1000) 
b = BankAccount(90,40,1000) 
a.deposit = 90 
b.deposit = 90 
b.withdraw = 40 
a.withdraw = 1000 

class MinimumBalanceAccount(BankAccount): 
    def __init__(self, minimum_balance=50): 
    BankAccount.__init__(self) 
    self.minimum_balance = minimum_balance 

    def minimum_balance(self): 
     self.minimum_balance = minimum_balance 
     def withdraw(self, amount): 
     if self.balance - amount < self.minimum_balance: 
      print 'Sorry, minimum balance must be maintained.' 

Ich versuche, das Programm, sondern ein interner Fehler wird angezeigt, mit folgender Syntax Interner Fehler auszuführen:Python init() Fehler

THERE IS AN ERROR/BUG IN YOUR CODE 
Results: 
Internal Error: runTests aborted: TestOutcomeEvent(handled=False, test=, result=, outcome='error', exc_info=(, TypeError('__init__() takes exactly 4 arguments (2 given)',),), reason=None, expected=False, shortLabel=None, longLabel=None) is not JSON serializable 

ist die Frage:

Erstellen Sie eine Klasse 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 
genannt

dies ist der Testcode:

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

Ich weiß nicht, was die Ursache ist.

Antwort

0

In AccountBalanceTestCases versuchen Sie, BankAccount instanziieren, indem Sie einen Parameter übergeben (Einzahlung = 90). Die Methode __init__ erwartet dagegen zwei weitere Argumente: Zurückziehen und Saldo. während der Ausführung

Dies verursacht den folgenden Fehler:

init() takes exactly 4 arguments (2 given) 

Es bedeutet, dass init zwei Argumente empfangen (die implizite ‚Selbst‘ und die, die Sie übergeben), aber es erwartet tatsächlich 4. Versuchen um zwei weitere Werte den Bau, die so etwas wie:

self.my_account = BankAccount(90, 100, 110) 

Dies wird Ihnen nach diesem Fehler erhalten ...

Verwandte Themen