Python 实例篇
功能概要
实现简单的超市商品展示、添加、移除,选择性操作,购买结算的输入输出等。
知识点
-条件判断
-循环遍历
-输入输出
-方法定义调用
-基本数据类型
运行截图
运行截图-1 运行截图-2案例代码
'''
Created on Oct 8, 2019
@author: Darker
控制台超市系统V1.0
'''
#定义历史商品数量
size = 10
#定义仓库
repository = dict()
#定义购物清单
shop_list = []
#定义一个函数来初始化商品
def init_repsitory():
#初始化很多商品
for g in range(1,size):
#商品编码
code = "G"+str(10000+g)
good = (code,"商品"+str(code),100)
repository[code] = good
#展示仓库商品信息
def show_repsitory():
print("Welcome")
print("The Goods List :")
title = ("商品编码","商品名称","价格¥")
print("%30s%30s%38s" % title)
title = ("——————","——————————","————")
print("%15s%16s%12s" % title)
for good in repository.values():
print("%15s%15s%12s" % good)
#显示订单
def show_list():
sum = 0#记录总额
print("=" * 100)
if not shop_list:
print("还未购买任何商品")
else:
title = "%-5s%-10s%20s%50s%15s%20s" % ("ID","条码" ,"商品名称 ","单价","数量","小计")
print(title)
for l in shop_list:
sum += l[5]
print("%-5s%-10s%5s%16s%6s%10s" % l)
print("=" * 100)
print((" " * 100)+"订单总价:"+str(sum))
operator()
#展示基本操作
def operator():
x = input("What You Want To Do? \n1-Remove 2-Cancel 3-Shoping 4-Checkout 5-Break 6-ShowRepsitory:\n")
if x == "1":
if shop_list.__len__() == 0:
print("没有购买任何商品")
catching()
i = int(input("Input ID What You Want To Remove? \n"))
if i > shop_list.__len__() or i < 0:
print("Not Exists ID")
catching()
else :
item = shop_list[i-1]
remove(item)
elif x == "2":
print("Cancel And ShowList")
show_list()
elif x == "3":
shoping()
elif x == "4":
print("订单结算,欢迎下次光临")
elif x == "5":
print("结束购物,欢迎下次光临")
elif x == "6":
show_repsitory()
show_list()
else:
catching()
#捕获异常提示通用处理
def catching():
x = input("Input Not suported ,What you Want? 1-Shoping 2-Break 3-ShowList\n")
if x == "1":
shoping()
elif x == "2":
print("Shoping is Down!!!")
elif x =="3":
show_list()
else:
catching()
#移除商品
def remove(value):
shop_list.remove(value)
show_list()
#购买商品
def shoping():
index = shop_list.__len__()
code = input("Please Input Goods Code:\n")
if code not in repository:
code = input("Code is not Real,Pleas Agin:\n")
if code not in repository:
code = input("Code is not Real,Pleas Agin:\n")
if code not in repository:
print("You had Check None 3's")
x = input("Did You Want To Break? Y/N:")
if x == "Y" or x == "y":
return
elif x == "N" or x == "n":
shoping()
#根据条形码找到商品
goods = repository[code]
#确定购买的件数
num = input("Please Input Num Did you Want:\n")
print("Picked : " + goods[1]+" 份数:"+str(num))
#订单格式
# ID,编码,名称,单价,份数,小计
shop_list.append((index+1,code,goods[1],goods[2],int(num),(goods[2]*int(num))))
x = input("Did You Want To Continue? Y/N:\n")
if x == "Y" or x == "y":
shoping()
elif x == "N" or x == "n":
show_list()
else:
catching()
#运行主程序
if __name__ == '__main__':
init_repsitory()
show_repsitory()
shoping()
待后续有机会和灵感的时候更新
网友评论