class Acccount(object): def __init__(self): self.balance = None def __int__(self,id,balance): self.id = id self.balabce = balance def deposit(self,inMoney): try: assert inMoney > 0 self.balabce = inMoney + inMoney print("你向账户成功存入"+str(inMoney)+"元宝") except EOFError: print("存入钱数必须大于零") def withdraw(self, amount): try: assert amount > 0 assert amount <= self.balance self.balance -= amount print("你从账户成功取出" + str(amount) + "元宝") except AssertionError: if amount < 0: print("取出钱数必须大于零") else: print("余额不足")为这段添加详细注释
时间: 2024-02-15 11:27:25 浏览: 82
# 定义一个 Acccount 类
class Acccount(object):
# 初始化方法,设置账户余额
def __init__(self):
self.balance = None
# 定义一个构造方法,设置账户id和余额
def __int__(self,id,balance):
self.id = id
self.balabce = balance
# 存款方法,inMoney 为存款数目
def deposit(self,inMoney):
try:
# 判断存款数目是否大于零
assert inMoney > 0
# 更新账户余额
self.balabce = inMoney + inMoney
# 输出存款成功信息
print("你向账户成功存入"+str(inMoney)+"元宝")
except EOFError:
# 存款数目小于等于零,输出错误信息
print("存入钱数必须大于零")
# 取款方法,amount 为取款数目
def withdraw(self, amount):
try:
# 判断取款数目是否大于零,且小于等于账户余额
assert amount > 0
assert amount <= self.balance
# 更新账户余额
self.balance -= amount
# 输出取款成功信息
print("你从账户成功取出" + str(amount) + "元宝")
except AssertionError:
# 取款数目小于等于零或者账户余额不足,输出错误信息
if amount < 0:
print("取出钱数必须大于零")
else:
print("余额不足")
阅读全文