2014-11-03 45 views
-2

我想在python中編寫這些行。BankAccount在Python中,得到了錯誤的結果

class BankAccount: 
    def __init__(self, initial_balance): 
     """Creates an account with the given balance.""" 
     … 
    def deposit(self, amount): 
     """Deposits the amount into the account.""" 
     … 
    def withdraw(self, amount): 
     """ 
     Withdraws the amount from the account. Each withdrawal resulting in a 
     negative balance also deducts a penalty fee of 5 dollars from the balance. 
     """ 
     … 
    def get_balance(self): 
     """Returns the current balance in the account.""" 
     … 
    def get_fees(self): 
     """Returns the total fees ever deducted from the account.""" 
    … 

「的存款和退出方法都更改帳戶餘額。該撤方法還扣除5美元的費用從如果撤出(之前任何費用)爲負平衡的結果。因爲我們也有平衡方法get_fees,你將需要一個變量來跟蹤支付的費用

這是一個可能的測試,它應該分別打印值10和5,因爲取款需要支付5美元 「

但我得到了一個錯誤的結果,第一個確定但第二個結果是錯誤的 任何意見請
這是我的代碼

## python BankAccount program################# 

fees = 0 
class BankAccount: 
     def __init__(self, init_bal): 

     """Creates an account with the given balance.""" 
     self.init_bal = init_bal 
     self.account = init_bal 
     self.fees = fees 


    def deposit(self, amount): 

     """Deposits the amount into the account.""" 
     self.amount = amount 
     self.account += amount 


    def withdraw(self, amount): 

     """ 
     Withdraws the amount from the account. Each withdrawal resulting in a 
     negative balance also deducts a penalty fee of 5 dollars from the balance. 
     """ 

     self.account -= amount 
     if self.account < 0: 
      self.account -= 5 

     self.fees += 5 




    def get_balance(self): 

     """Returns the current balance in the account.""" 
     return self.account 

    def get_fees(self): 

     """Returns the total fees ever deducted from the account.""" 

     return self.fees 


###### First account ###########################  
my_account_1 = BankAccount(10) 
my_account_1.withdraw(15) 
my_account_1.deposit(20) 
my_account_1.withdraw(25) 
print my_account_1.get_balance(), my_account_1.get_fees() 
####### second account######################## 
my_account = BankAccount(10) 
my_account.withdraw(5) 
my_account.deposit(10) 
my_account.withdraw(5) 
my_account.withdraw(15) 
my_account.deposit(20) 
my_account.withdraw(5) 
my_account.deposit(10) 
my_account.deposit(20) 
my_account.withdraw(15) 
my_account.deposit(30) 
my_account.withdraw(10) 
my_account.withdraw(15) 
my_account.deposit(10) 
my_account.withdraw(50) 
my_account.deposit(30) 
my_account.withdraw(15) 
my_account.deposit(10) 
my_account.withdraw(5) 
my_account.deposit(20) 
my_account.withdraw(15) 
my_account.deposit(10) 
my_account.deposit(30) 
my_account.withdraw(25) 
my_account.withdraw(5) 
my_account.deposit(10) 
my_account.withdraw(15) 
my_account.deposit(10) 
my_account.withdraw(10) 
my_account.withdraw(15) 
my_account.deposit(10) 
my_account.deposit(30) 
my_account.withdraw(25) 
my_account.withdraw(10) 
my_account.deposit(20) 
my_account.deposit(10) 
my_account.withdraw(5) 
my_account.withdraw(15) 
my_account.deposit(10) 
my_account.withdraw(5) 
my_account.withdraw(15) 
my_account.deposit(10) 
my_account.withdraw(5) 
print my_account.get_balance(), my_account.get_fees() 

the results are : -20 10 (first result) 
        -60 115 (second result) 
+0

_「但我得到了一個錯誤的結果」_。什麼是正確的結果? – Kevin 2014-11-03 12:56:51

回答

0
def withdraw(self, amount): 

    """ 
    Withdraws the amount from the account. Each withdrawal resulting in a 
    negative balance also deducts a penalty fee of 5 dollars from the balance. 
    """ 

    self.account -= amount 
    if self.account < 0: 
     self.account -= 5 

    self.fees += 5 

在這裏,你將5費無論賬戶是否被處罰與否。將最後一行放在if塊內。

if self.account < 0: 
     self.account -= 5 
     self.fees += 5 
+0

非常感謝凱文,是的,我錯過了,現在程序運行正常。 – NadNet 2014-11-03 13:02:40