项目需求:电影《我不是药神》上映后,口碑极高,一种名为”格列宁”的进口药为人们所熟知,医药话题也引起了人们热烈的讨论。下面按照要求定义一个药品Medicine类。
Medicine类的属性如下:
药名 name
价格 price
生产日期 PD
失效日期 Exp
Medicine类的方法如下:
获取药品名称 get_name()返回类型:str
计算保质期 get_GP()返回类型:str
计算药品是否过期 is_expire()返回类型:Bool
商品名称和生产日期只能查看不能修改
from datetime import datetime
class Medicine(object):
def __init__(self,name,price,PD,Exp):
self.__name = name
self.price = price
self.__PD = datetime.strptime(PD,'%Y-%m-%d')
self.Exp = datetime.strptime(Exp,'%Y-%m-%d')
@property
def get_name(self):
print('商品的名称为:',self.__name)
@property
def get_GP(self):
print('商品的保质期为:',(self.Exp - self.__PD).days)
@property
def get_PD(self):
print('商品的生产日期为:',self.__PD)
def is_expire(self):
if (datetime.today() - self.__PD).days > (self.Exp - self.__PD).days:
print('商品已经过期')
else:
print('商品没有过期')
if __name__ == '__main__':
medicineObj = Medicine('感冒胶囊',100,'2019-1-1','2019-3-1')
medicineObj.get_name
medicineObj.is_expire()
medicineObj.get_GP
datetime模块
from datetime import datetime
start_date = '2019-01-2'
end_date = '2019-12-31'
start = datetime.strptime(start_date,'%Y-%m-%d')
end = datetime.strptime(end_date,'%Y-%m-%d')
# print(type(datetime.strptime(end - start),'%Y-%m-%d'))
print(type((end-start).days))
# delta_days = end - start
datetime.timedelta(days=338)
# from datetime import date
项目需求:#银行账户资金交易管理
用类和对象实现一个银行账户的资金交易管理,包括存款和打印交易详情,
交易详情中包含每次交易的时间、存款或者取款的金额、每次交易后的余额
下面按照要求定义一个账户Account类
账户Account类的属性:
当前账户金额 money
当前账户交易日志 account_logs
账户Account类的方法:
存钱 deposit()无返回值
取钱 withdraw()无返回值
打印交易详情 transaction_log()无返回值
from prettytable import PrettyTable
from datetime import datetime
# money = 0
#account_logs = []
class Account(object):
def __init__(self,money=0,account_logs=[]):
self.money = money
self.account_logs = account_logs
def deposit(self,deposit_money):
self.money = self.money+deposit_money
self.account_logs.append([deposit_money,'存钱'])
print('当前余额为:',(self.money))
def withdraw(self,wd_money):
if wd_money > self.money:
print('余额不足')
else:
self.money = self.money-wd_money
self.account_logs.append([wd_money,'取钱'])
print('当前余额为:',(self.money))
def transaction_log(self):
field_name = ('交易日期','摘要','金额','币种','余额')
table = PrettyTable(field_names=field_name)
for info in self.account_logs:
if info[1] == '存钱':
info[0] = '+{}'.format(info[0])
else:
info[0] = '-{}'.format(info[0])
row = [datetime.today(),info[1],info[0],'人民币',self.money]
table.add_row(row)
print(table)
# account = Account(100,[])
# account.deposit(100)
# print(account.account_logs)
# account.withdraw(200)
# account.transaction_log()
def show_menu():
"""显示菜单栏"""
menu = """
=====================银行账户资金交易管理===================
0:退出
1:存款
2:取款
3:打印交易详情
=========================================================
"""
print(menu)
if __name__ == '__main__':
show_menu()
account = Account()
while True:
choice = int(input("请输入你的选择:"))
if choice == 0:
exit(0)
print('退出系统')
elif choice == 1:
flag = True
while flag:
deposit_money = float(input('存款金额为;'))
account.deposit(deposit_money)
flag = True if input('是否继续存款Y|N?').lower()=='y' else False
elif choice == 2:
flag = True
while flag:
wd_money = float(input('取款金额为;'))
account.withdraw(wd_money)
flag = True if input('是否继续取款Y|N?').lower() == 'y' else False
elif choice == 3:
account.transaction_log()
else:
print('请选择正确的编号')