私はこれらの行を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メソッドもあるため、次のようになります。支払った料金を追跡するための変数を持つこと。
これがクラスの1つの可能なテストです。引き出しには5ドルの手数料がかかるため、それぞれ10と5の値を出力する必要があります。「」
しかし、私は間違った結果を得ました、最初は大丈夫ですが、2番目の結果は間違っていますアドバイスしてください
これは私のコードです
## 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)
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
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加