补全Account和FreeChecking类的实现 class Account: """An account has a balance and a holder. >>> a = Account('John') >>> a.deposit(12) 12 >>> a.balance 12 >>> a.interest 0.02 >>> a.time_to_retire(20) 26 >>> a.balance # balance 保持不变 12 >>> a.withdraw(11) # 每次取款额度不超过10,超过无法取出 "Can't withdraw that amount" >>> a.withdraw(10) 2 >>> a.withdraw(10) 'Insufficient funds' """ def __init__(self, account_holder): def deposit(self, amount): def withdraw(self, amount): def time_to_retire(self, amount): """Return the number of years until balance would grow to amount.""" assert self.balance > 0 and amount > 0 and self.interest > 0 class FreeChecking(Account): """A bank account that charges for withdrawals, but the first two are free! >>> ch = FreeChecking('Jack') >>> ch.balance = 20 >>> ch.withdraw(100) # 首次取款免费,不论是否成功,免费次数减少1次 'Insufficient funds' >>> ch.withdraw(3) # 第二次取款免费 17 >>> ch.balance 17 >>> ch.withdraw(3) # 2次免费用完,开始收费 13 >>> ch.withdraw(3) 9 >>> ch2 = FreeChecking('John') >>> ch2.balance = 10 >>> ch2.withdraw(3) # 首次免费 7 >>> ch.withdraw(3) # 2次免费 5 >>> ch.withdraw(5) # 余额不足 'Insufficient funds' """ def __init__(self, account_holder): def withdraw(self, amount): import doctest doctest.testmod()
时间: 2023-06-24 22:05:50 浏览: 143
安卓A-Z字母排序索引相关-AndroidListView侧栏字母条索引定位.rar
class Account:
"""An account has a balance and a holder.
>>> a = Account('John')
>>> a.deposit(12)
12
>>> a.balance
12
>>> a.interest
0.02
>>> a.time_to_retire(20)
26
>>> a.balance # balance 保持不变
12
>>> a.withdraw(11) # 每次取款额度不超过10,超过无法取出
"Can't withdraw that amount"
>>> a.withdraw(10)
2
>>> a.withdraw(10)
'Insufficient funds'
"""
def __init__(self, account_holder):
self.holder = account_holder
self.balance = 0
self.interest = 0.02
def deposit(self, amount):
self.balance += amount
return self.balance
def withdraw(self, amount):
if amount > 10:
return "Can't withdraw that amount"
elif self.balance < amount:
return 'Insufficient funds'
else:
self.balance -= amount
return self.balance
def time_to_retire(self, amount):
assert self.balance > 0 and amount > 0 and self.interest > 0
years = 0
while self.balance < amount:
self.balance *= (1 + self.interest)
years += 1
return years
class FreeChecking(Account):
"""A bank account that charges for withdrawals, but the first two are free!
>>> ch = FreeChecking('Jack')
>>> ch.balance = 20
>>> ch.withdraw(100) # 首次取款免费,不论是否成功,免费次数减少1次
'Insufficient funds'
>>> ch.withdraw(3) # 第二次取款免费
17
>>> ch.balance
17
>>> ch.withdraw(3) # 2次免费用完,开始收费
13
>>> ch.withdraw(3)
9
>>> ch2 = FreeChecking('John')
>>> ch2.balance = 10
>>> ch2.withdraw(3) # 首次免费
7
>>> ch.withdraw(3) # 2次免费
5
>>> ch.withdraw(5) # 余额不足
'Insufficient funds'
"""
def __init__(self, account_holder):
super().__init__(account_holder)
self.free_withdrawals = 2
self.withdrawal_fee = 1
def withdraw(self, amount):
if self.free_withdrawals > 0:
self.free_withdrawals -= 1
return super().withdraw(amount)
else:
return super().withdraw(amount + self.withdrawal_fee)
import doctest
doctest.testmod()
阅读全文