小伙伴们,用类和对象实现自己银行账户的资金交易管理吧,包括存款、取款和打印交易详情,交易详情中包含每次交易的时间、存款或者取款的金额、每次交易后的余额。

小慕提醒:一定要先思考哦,实在没有思路再去看参考答案!!!

参考答案
# coding:utf-8
import time
class BankAccount(object):
def __init__(self, balance):
self.balance = balance # 账户初始余额
print("您的初始余额: {}元".format(self.balance))
# 存款
def deposit(self, deposit_time, deposit_money):
self.balance += deposit_money
print("存入时间: {}, 存入金额: +{}元, 余额: {}元".format(deposit_time, deposit_money, self.balance))
# 消费
def withdraw(self, withdraw_time, withdraw_money):
self.balance -= withdraw_money
print("消费时间: {}, 消费金额: -{}元, 余额: {}元".format(withdraw_time, withdraw_money, self.balance))
if __name__ == '__main__':
xiaomu = BankAccount(800)
xiaomu.deposit(time.strftime('%Y-%m-%d %H:%M:%S'), 1000)
time.sleep(1)
xiaomu.deposit(time.strftime('%Y-%m-%d %H:%M:%S'), 5500)
time.sleep(2)
xiaomu.withdraw(time.strftime('%Y-%m-%d %H:%M:%S'), 1200)