码迷,mamicode.com
首页 > 编程语言 > 详细

python3之模拟商场购物

时间:2019-12-28 22:58:11      阅读:124      评论:0      收藏:0      [点我收藏+]

标签:列表   商品列表   消费   git   cart   coding   div   ping   float   

模拟商场购物

输入部分需要进行输入有效性校验,原谅我技术还不到位

  1 #coding:utf-8
  2 
  3 #商品信息
  4 shop = {
  5         1:{"品名":"苹果","单价":6,"单位":"Kg"},
  6         2:{"品名":"","单价":3,"单位":"Kg"},
  7         3:{"品名":"可乐","单价":2.5,"单位":""}
  8         }
  9 
 10 #购物车{商品编号:商品数量}
 11 shoppingCart = {}
 12 
 13 money = 50
 14 
 15 #打印商店商品列表
 16 def printShop():
 17     for key1 in shop.keys():
 18         print("商品编号:",key1,"\t品名:",shop.get(key1).get("品名"),"\t单价:",shop.get(key1).get("单价"),"元/",shop.get(key1).get("单位"))
 19 
 20 #打印购物车商品列表
 21 def printShoppingCart():
 22     for key1 in shoppingCart.keys():
 23         print("商品编号:",key1,"\t品名:",shop.get(key1).get("品名"),"\t数量:",shoppingCart.get(key1),shop.get(key1).get("单位"),"\t单价:",shop.get(key1).get("单价"),"元/",
 24               shop.get(key1).get("单位"))
 25 
 26 #购买操作
 27 def buy(id,num):
 28     #错误校验
 29     if not id in shop.keys():
 30         print("商品编号有误")
 31         return
 32     num1 = 0
 33     #购物车已有商品则获取原有商品数量
 34     if shoppingCart.get(id) != None:
 35         num1 = shoppingCart.get(id)
 36     re = num + num1
 37     shoppingCart.update({id:re})
 38 
 39 #删除商品
 40 def dele(id,num):
 41     #错误校验
 42     if id not in shoppingCart.keys():
 43         print("要删除的商品不存在")
 44         return
 45     if num>shoppingCart.get(id) or num<0:
 46         print("数量有误")
 47         return
 48     if num==0:
 49         return
 50     re = shoppingCart.get(id) - num
 51     if re == 0:
 52         shoppingCart.pop(id)
 53     else:
 54         shoppingCart.update({id:re})
 55     print("删除成功")
 56 
 57 #统计购物车当前总金额
 58 def totalMoney():
 59     totalMoney1 = 0
 60     for id,num in shoppingCart.items():
 61         totalMoney1 += (num*shop.get(id).get("单价"))
 62     return totalMoney1
 63 
 64 #结算操作
 65 def pay(money):
 66         #购物车中无商品
 67     if totalMoney() == 0:
 68         print("请先选购商品再进行结算")
 69         return
 70 
 71     #商品总价高于卡中金额
 72     if totalMoney() > money:
 73         print("卡余额不足,结算失败")
 74         return
 75 
 76     #结算
 77     money -= totalMoney()
 78     print("您本次共消费",totalMoney(),"元,卡上余额:",money)
 79 
 80 #选购商品
 81 def xgsp():
 82     # print("选购商品")
 83     while True:
 84         id = -1
 85         num = 0
 86         printShop()
 87         ss = input("请输入要选购商品的商品编号,返回请按q:")
 88         if ss == "q":
 89             return
 90         elif not ss.isdigit() or (int)(ss) not in shop.keys():
 91             print("输入的编号有误")
 92             continue
 93         else:
 94             id = (int)(ss)
 95             num = (float)(input("请输入购买数量:"))
 96         buy(id,num)
 97         print("购买成功")
 98         continue
 99 
100 #查看购物车
101 def ckgwc():
102     # print("查看购物车")
103     if totalMoney()==0:
104         print("当前购物无商品")
105         return
106     while True:
107         print()
108         printShoppingCart()
109         print("1、结账")
110         print("2、删除商品")
111         cho = input("请选择操作,输入q返回:")
112         if cho == "q":
113             return
114         elif cho == "1":
115             pay(money)
116             return
117         elif cho == "2":
118             id = (int)(input("请输入商品编号:"))
119             num = (int)(input("请输入删除数量:"))
120             dele(id,num)
121         else:
122             print("输入有误")
123         print()
124 
125 def jz():
126     pay(money)
127     # print("结账")
128     return
129 
130 
131 #主程序
132 while True:
133     print()
134     print("1、选购商品")
135     print("2、查看购物车")
136     print("3、结账")
137     cho = input("请选择操作,退出请输入q:")
138     if cho == "q":
139         print("欢迎下次光临")
140         break
141     elif cho == "1":
142         xgsp()
143     elif cho == "2":
144         ckgwc()
145     elif cho == "3":
146         jz()
147     else:
148         print("输入有误")
149     print()

python3之模拟商场购物

标签:列表   商品列表   消费   git   cart   coding   div   ping   float   

原文地址:https://www.cnblogs.com/exception999/p/12113174.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!