任务:
在倒计时内,完成所有商品的保质期检测
新增知识点:
- 模块是一个包含所有你定义的函数和变量的文件,其后缀名是.py
- 引入模块:import 模块名
- 使用datetime模块当中的方法:datetime.datetime(年, 月, 日)
- 对象.属性
- if...else...语句:
- if...elif...else...语句:
- 通过Scratch积木,为Python变量赋值
案例流程图:
python核心代码:
# 10% 禁止
# 30% 0.5
# 50% 0.8
# 50%以上 原价
import datetime
now_date = datetime.datetime(2038,8,8) # 当前日期
pro_date = datetime.datetime(year,month,day) # 生产日期
gap = now_date - pro_date # 当前日期和生产日期相差天数
remain = exp - gap.days # 剩余保质天数
if remain < exp * 0.1: # 天数少于10%就禁止出售
print('已过期,禁止销售')
elif remain < exp * 0.3: # 天数少于30%就打5折
print('5折')
elif remain < exp * 0.5: # 天数少于50%就打8折
print('8折')
else: # 否则原价
print('原价')
scratch核心脚本:
1.在背景中搭建积木,初始化python中的生产日期和保质期
2.运行scratch程序,点击商品,查看生产日期和保质期,填入下面图片中的第三块积木中
比如“可乐”的核心脚本
作业:
1.剩余的商品的scratch脚本添加好