zoukankan      html  css  js  c++  java
  • 购物车第一版

    #!/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
  • 相关阅读:
    Winform 打包,卸载程序制作获取ProductCode
    委托/事件的重写
    反序列化无法找到程序集
    Control.Invoke注意事项
    操作config文件
    MemoEdit自动滚动到结尾
    读取局域网内的共享文件
    命令行卸载程序
    获取执行程序的路径
    SCSF 系列:使用 Visualizer 监控 SCSF 运行状态
  • 原文地址:https://www.cnblogs.com/godspeed034/p/6892593.html
Copyright © 2011-2022 走看看