#!/usr/bin/env python # -*- coding:utf-8 -*- # by wk import sys def mycarlist(mygoodsindex,glist,userinfo): #购物车列表,同时返回新的商品列表 newglist = goodssele(mygoodsindex,glist) for index, item in enumerate(newglist): if mygoodsindex == index: # print('my add',newglist[index]) mycar = newglist[index] pmoney = personmoney(userinfo,newglist[index]['price']) # print('my add2',mycar) return mycar,newglist,pmoney def goodsdb(glist): #商品数据库 goodsdatabase goodslist = glist return goodslist def goodssele(goodsindex,glists): #选择好的商品自动减1 glist = goodsdb(glists) for index, item in enumerate(glist): # print(index,item) if goodsindex == index: nu = int(glist[index]['num']) -1 glist[index].update({'num':nu}) # print(glist[index]) return glist def personmoney(userinfo,goodsmoney): #个人有多少钱 if int(userinfo[0]['mymoney']) > int(goodsmoney): userinfo[0]['mymoney'] = int(userinfo[0]['mymoney']) - int(goodsmoney) else: print('not enougth money') return userinfo[0]['mymoney'] if __name__ == '__main__': # 商品列表 goodslist = [{'gid': '1', 'gname': 'mobile', 'price': 1000, 'num': 10}, {'gid': '2', 'gname': 'laptop', 'price': 2000, 'num': 10}, {'gid': '3', 'gname': 'desktop', 'price': 3000, 'num': 10}] mycatlist = [] userinfo = [{'uname':'wk','mymoney':10000}] # goout = 0 while True: glist = goodsdb(goodslist) print('id goods num') for i in glist: print(i['gid'], i['gname'], i['num']) print('select e to exit') mygoodsindex = input('Please select:') if mygoodsindex == 'e': #选择e退出 print('mycat',mycatlist) print('goodslist',goodslist) print('usermoney',userinfo) break else: rmygoodsindex = int(mygoodsindex) - 1 # mylist,newglist = mycarlist(rmygoodsindex) mylist,newglist,pmoney = mycarlist(rmygoodsindex,glist,userinfo) print('my add',mylist) print('my add 2',newglist) mycatlist.append(mylist) #添加到购物车 goodslist = newglist #更新商品列表 userinfo[0]['mymoney'] = pmoney