zoukankan      html  css  js  c++  java
  • 8.09_python_lx_shopping

    购物车小程序

    (1)购物车程序

    money = 0
    import time,json
    car = {}
    
    def chongzhi():
        while True:
            global money
            qian = input('请充值吧,大哥')
            if qian.isdecimal():
                qian = int(qian)
                money+=qian
                print('恭喜你~ 充值成功{}元人民币'.format(qian))
                break
            else:
                print('充值失败,非数字.')
    
    def jindutiao():
        for i in range(51):
            print('
    {}{}%%'.format('*'*i,int(i/50*100)),end='')
            time.sleep(0.05)
    
    def londing():
        print('加载商品中 ... ')
        jindutiao()
        print()
    
    def read_goods(file):
        with open(file,mode='r',encoding='utf-8')as fp:
            lst  = []
            for i in fp:
                dic = json.loads(i)
                lst.append(dic)
            return lst
    goods_lst = read_goods('shopping_data.json')
    print(goods_lst)
    
    def goods():
        strvar = '商品名称'.center(20)
        strvar = '序号'+strvar+'价格'
        print(strvar)
        for k,v in enumerate(goods_lst,start=1):
            v['num'] = k
            print('{v[num]:<12}{v[name]:<12}{v[price]}'.format(v=v))
    
    def add_goodscar(num):
        if num not in car:
            car[num] = {'name':goods_lst[num-1]['name'],'price':goods_lst[num-1]['price'],'account':1}
        elif num in car:
            car[num]['account']+=1
        print(car)
    
    def show_goodscar(num):
        print('*'*50)
        print('您选择的商品具体信息:')
        print('*-商品名称:{}'.format(car[num]['name']))
        print('*-商品单价:{}'.format(car[num]['price']))
        print('*-商品数量:{}'.format(car[num]['account']))
        print('已成功添加到购物车~ 请继续shopping ~')
        print('*'*50)
    
    def jiesuan():
        total = 0
        print('[-------------------您购物车的具体商品如下:-------------------]')
        for k,v in car.items():
            v['zongjia'] = v['price']*v['account']
            total+=v['zongjia']
            v['num'] = k
            print('序号{v[num]}: 商品名称:{v[name]} 商品单价:{v[price]} 商品数量:{v[account]} 此商品总价:{v[zongjia]}'.format(v=v))
        return total
    
    def del_goodscar(total,money):
        print('余额不足,还差{}元,请忍痛割爱,删除某些商品'.format(total-money))
        del_num = input('[-------------------请输入要删除的商品序号:-------------------]')
        if del_num.isdecimal():
            del_num = int(del_num)
            if del_num in car:
                car[del_num]['account']-=1
                if not car[del_num]['account']:
                    car.pop(del_num)
            else:
                error()
        else:
            error()
    
    def success(total,money):
        print('正在结算数据中 ... ')
        jindutiao()
        print('[一共:{}元]'.format(total))
        print('[您已经成功购买以上所有商品 , 余额还剩{}元,感谢您下次光临~]'.format(money-total))
            
    def myexit():
        print('[============== 欢迎下次光临: ==============]')
        time.sleep(0.5)
    
    def error():
        print('这是一个错误选项')
    
    def main():
        chongzhi()
        # londing()
        goods()
        sign = True
        while sign == True:
            num = input('请输入您要购买的商品:')
            if num.isdecimal():
                num = int(num)
                if 0 < num <= len(goods_lst):
                    add_goodscar(num)
                    show_goodscar(num)
                else:
                    error()
            
            elif num.upper()=='N':
                while True:
                    total = jiesuan()
                    if total>money:
                        del_goodscar(total,money)
                    else:
                        success(total,money)
                        sign = False
                        break
            elif num.upper()=='Q':
                myexit()        
                sign = False
            else:
                error()
    main()

    (2)商品文件内容 json文件

    {"name":"电脑","price":1999}
    {"name":"鼠标","price":10}
    {"name":"游艇","price":20}
    {"name":"美女","price":998}
    {"name":"风油精","price":30}
  • 相关阅读:
    HTTP/HLS/RTMP超级负载测试工具(转)
    Jmeter监控Linux服务器性能
    装饰器做缓存
    内置装饰器
    Python装饰器 计时器记录方法执行性能
    【Python】装饰器实现日志记录
    Java对关于两个地点的根据经纬度算出后排序
    JS获得当前位置信息
    百度地图插件开发使用三 及jquery function(a.b)排序等
    css用clearfix清除浮动
  • 原文地址:https://www.cnblogs.com/Magicianlx/p/13466682.html
Copyright © 2011-2022 走看看