首页 > 代码库 > 带用户登录的购物车

带用户登录的购物车

技术分享

 

  1 #!/usr/bin/env python
  2 # -*- coding: utf-8 -*-
  3 import getpass
  4 import sys
  5 f = open(user.db,r)
  6 welcome="欢迎来欢乐多购物平台"
  7 out = 谢谢惠顾小店
  8 user_money = 5000000
  9 shopping_list = []
 10 shopping_car = {}
 11 chose_list = []
 12 goods = [
 13     {"name": "电脑", "price": 1999},
 14     {"name": "鼠标", "price": 10},
 15     {"name": "游艇", "price": 20},
 16     {"name": "手机", "price": 6000},
 17     {"name": "手表", "price": 5000},
 18     {"name": "香烟", "price": 100},
 19     {"name": "啤酒", "price": 200},
 20     {"name": "音响", "price": 300},
 21     {"name": "啤酒", "price": 400},
 22     {"name": "红酒", "price": 1000},
 23     {"name": "牛奶", "price": 600},
 24     {"name": "雪碧", "price": 700},
 25     {"name": "饮水机", "price": 800},
 26     {"name": "机械键盘", "price": 3000},
 27     {"name": "钱包", "price": 900},
 28     {"name": "刮胡刀", "price": 1100},
 29     {"name": "充电宝", "price": 50},
 30     {"name": "咖啡", "price": 1200},
 31     {"name": "眼镜", "price": 1300},
 32     {"name": "电脑包", "price": 1400},
 33     {"name": "电视机", "price": 4000},
 34 
 35 ]
 36 data =http://www.mamicode.com/ f.read()
 37 f.close()
 38 user_db_list = []
 39 user_info_list = data.split(\n)
 40 print(user_info_list)
 41 for item in user_info_list:
 42     temp = item.split(|)
 43     v = {
 44         name: temp[0],
 45         password: temp[1],
 46         number: temp[2],
 47     }
 48     user_db_list.append(v)
 49 print(welcome)
 50 p = 0
 51 while True:
 52     user_name = input(请输入您的账号:)
 53     if user_db_list:
 54         user_password = input(请输入您的密码:)
 55     for index,user_list in enumerate(user_db_list):
 56         if user_name == user_list[name] and int(user_list[number]) < 3:
 57             if user_password == user_list[password]:
 58                 print(欢迎,user_name,登录我们平台,账号金额:,user_money)
 59                 one_goods = goods[0:5]
 60                 for l in one_goods:
 61                     print(l)
 62                 print(1页/6页)
 63                 while True:
 64                     s = input(请输入页码:)
 65                     s = int(s)
 66                     start = (s - 1) * 5
 67                     end = s * 10
 68                     one_goods = goods[start:end]
 69                     for i in enumerate(one_goods):
 70                         shop_id = i[0]
 71                         shop_name = i[1].get(name)
 72                         shop_money = i[1].get(price)
 73                         print(商品id:, shop_id, 商品:, shop_name, 售价:, shop_money)
 74                     enterone = input(请问你需要选购么?y/n)
 75                     if enterone.lower() and enterone == y:
 76                         print(请您输入你想要的商品id,选购完毕离开请输入Q/q)
 77                         while True:
 78                             choose = input(上帝的选择:)
 79                             if choose.lower() and choose == q:
 80                                 break
 81                             elif int(choose) > int(len(one_goods)):
 82                                 print(亲爱的上帝,您输入的数字不在商品列表中,请重新输入)
 83                                 continue
 84                             else:
 85                                 now_money = user_money
 86                                 user_money = user_money - goods[int(choose)].get(price)
 87                                 if now_money <= goods[int(choose)].get(price):
 88                                     print(您的账号余额:, now_money, 购买:, goods[int(choose)], 还差:, user_money)
 89                                     # print(‘加入购物车失败!‘)
 90                                     print("""
 91                                     1  充值
 92                                     2  删减购物车
 93                                     """)
 94                                     now_chose = input(请选择:1/2)
 95                                     if int(now_chose) == 1:
 96                                         recharge_money = int(input(请输入充值金额:))
 97                                         user_money = user_money + recharge_money
 98                                         print(您的账号金额:, user_money)
 99                                     elif int(now_chose) == 2:
100                                         for w in enumerate(chose_list):
101                                             print(w)
102                                         while True:
103                                             remove_list = input(请输入您想要删除的商品id:退出请输入Q/q)
104                                             now_list = int(len(chose_list))
105                                             if remove_list.lower() and remove_list == q:
106                                                 print(已成功返回上一层:)
107                                                 break
108                                             elif int(remove_list) < now_list:
109                                                 g = None
110                                                 e = chose_list[int(remove_list)]
111                                                 for g in enumerate(goods):
112                                                     if str(e) in g[1].get(name):
113                                                         user_money = now_money + g[1].get(price)
114                                                         print(您现在的余额为:, user_money)
115                                                 del chose_list[int(remove_list)]
116                                                 print(chose_list)
117                                             else:
118                                                 print(输入有误请重新输入)
119 
120                                 else:
121                                     chose_list.append(goods[int(choose)].get(name))
122                                     print(chose_list)
123                         print(chose_list)
124                     else:
125                         print(请稍候....,现在系统为你自动结算,)
126                         print(您的账号余额:, user_money)
127                         print(您购买的商品:, chose_list)
128                         shop_list = open(shopdata, w+,encoding=UTF-8)
129                         nstr =str(chose_list)
130                         shop_list.write(nstr)
131                         shop_list.close()
132                         # pass
133                         sys.exit()
134             else:
135                 print(账号密码错误,请重新输入)
136                 # data_read = open(‘user.db‘,‘r‘)
137                 # #
138                 #  data_write = ""
139                 #  data_write = user_list[‘name‘],user_list[‘number‘])= p
140                 break
141         else:
142             print(您输入的是未注册账号)
143             break
144     p += 1
145     if p == 3:
146         print(你的尝试超过限定次数,已退出)
147         break

 

带用户登录的购物车