我正在尝试在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 :(得分: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