1、任务1:银行账户资金交易管理
import timeimport prettytable as ptbalance = 1000 acount_log = [] class Bank: def __init__(self): """初始化""" global balance self.balance = balance self.acount_log = acount_log def deposit(self): """存款""" amount = float(input('请输入存款金额:')) self.balance += amount self.write_log(amount,'转入') def withdrawl(self): """取款""" amount = float(input('请输入取款金额:')) if amount > self.balance: print('余额不足') else: self.balance -= amount self.write_log(amount, '消费') def print_log(self): """打印交易日志""" tb = pt.PrettyTable() tb.field_names = ["交易日期", "摘要", "金额", "币种", "余额"] for info in self.acount_log: if info[1] == '转入': amout = '+{}'.format(info[2]) else: amout = '-{}'.format(info[2]) tb.add_row([info[0],info[1],amout,'人民币',info[3]]) print(tb) def write_log(self,amout,handle): """写入日志""" create_time = time.strftime('%Y-%m-%d %H:%M:%S', \ time.localtime(time.time())) data = [create_time, handle, amout, self.balance] self.acount_log.append(data) def show_menu(): """显示菜单""" menu = '''菜单 0:退出 1:存款 2:取款 3:打印交易详情 ''' print(menu)if __name__ == "__main__": show_menu() num = float(input('请根据菜单输入操作编号:')) bank = Bank() while num!= 0 : if num == 1: bank.deposit() elif num == 2: bank.withdrawl() elif num == 3: bank.print_log() else: print('您的输入有误!') num = float(input('请根据菜单输入操作编号:')) print('您已退出系统')

2、任务2:设计药品medicine 类
from datetime import datetimeclass Medicine: name = '' price = 0 PD = '' Exp = '' def __init__(self,name,price,PD,Exp): self.name = name self.price = price self.PD = PD self.Exp = Exp def get_name(self): return self.name def get_GP(self): start = datetime.strptime(self.PD, '%Y-%m-%d') end = datetime.strptime(self.Exp, '%Y-%m-%d') return (end-start).daysmedicine = Medicine(name='格列宁',price=1860,\ PD='2018-5-1',Exp='2018-12-1')name = medicine.get_name()GP = medicine.get_GP()print('药品名称:{}'.format(name))print('药品保质期:{}天'.format(GP))