我正在尝试找到一种方法来定义“ deposit”变量,以便根据输出的前一行得出不同的输出。
例如,如果代码的先前输出是:
Sorry, minimum balance must be maintained. Account is frozen
我希望以后的任何存款都不会影响余额,并打印:
Sorry, account frozen
这是我的代码:
class MinimumBalanceAccount:
def __init__(self,account_number,holder_name,min_balance):
self.account_number = account_number
self.holder_name = holder_name
self.balance = 0
self.min_balance = 200
def withdraw(self, amount):
self.balance -= amount
if self.balance < self.min_balance:
print("Sorry, minimum balance must be maintained. Account is frozen")
self.balance += amount
return self.balance
def deposit(self, amount):
self.balance += amount
if self.balance - amount < self.min_balance and self.balance - amount != 0:
print("Sorry, account frozen.")
self.balance -= amount
return self.balance + amount
我希望输出为:
Account: FR646 Paul
Depositing 500
Withdrawing 180
Withdrawing 180
Sorry, minimum balance must be maintained. Account is frozen
Depositing 500
Sorry, account frozen.
Balance: 320
鉴于此,我目前看到的是:
Account: FR646 Paul
Depositing 500
Withdrawing 180
Withdrawing 180
Sorry, minimum balance must be maintained. Account is frozen
Depositing 500
Balance: 820
答案 0 :(得分:1)
我认为您的两个函数都在一个类中。由于您未显示课程,因此我将其称为Account
。请注意,self
变量将始终是此类的实例。您可以向其添加任何字段。例如,您可以执行self.locked = True
来表明该帐户已被锁定。确保还包括一种__init__()
方法,用于将self.locked
初始化为适当的值。