博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
day5作业 写一个管理商品的函数
阅读量:7197 次
发布时间:2019-06-29

本文共 3008 字,大约阅读时间需要 10 分钟。

#商品管理的程序 ,商品都存在一个json串#  1新增商品    校验商品是否存在   校验价格是否合法#  2查询商品信息 校验商品是否存在#   3修改商品信息  校验商品是否存在#4  删除import jsonFILE_NAME ='goods.json'def op_file(name,content=None):    if content:        with open(name,'w',encoding='utf-8')as fw:    #写            json.dump(content,fw,indent=4,ensure_ascii=False)    else:        with open(name,encoding='utf-8')as fr:   #读            res = json.load(fr)            return resall_goods = op_file(FILE_NAME)def check_price(price):        #校验价格    price = str(price)    if price.isdigit():        price = int(price)        if price>0:            return True    else:        if price.count('.')==1:            tmp = price.split('.')            left = tmp[0]            right = tmp[1]            if left.isdigit() and right.isdigit()and int(right)>0:  #1.0                return True            elif left.isdigit() and right.isdigit()and int(left)>0:  #0.1                return True    return Falsedef get_good_info():    #商品信息    while True:        good_name = input('请输入商品名称:').strip()        price = input('请输入商品价格:').strip()        count = input('请输入商品数量:').strip()        color = input('请输入商品颜色:').strip()        if good_name and price and count and color:            if not check_price(price):                print('价格输入不合法,必须大于0')            elif not count.isdigit and int(count)<1:                print('商品数量不合法')            else:                return good_name,price,count,color        else:            print('输入不能为空!')def add_good():   #添加商品    good_name,price,count,color = get_good_info()    if good_name not in all_goods:        all_goods[good_name]={            'price':price,            'count':count,            'color':color        }        op_file(FILE_NAME,all_goods)        print('添加完成!')    else:        print('商品已经存在!')def update_good():   #修改商品    good_name,price,count,color = get_good_info()    if good_name in all_goods:        all_goods[good_name]={            'price':price,            'count':count,            'color':color        }        op_file(FILE_NAME,all_goods)        print('修改完成!')    else:        print('商品不存在!')def query_good():    #查询    good_name = input('请输入商品名称:').strip()    if good_name in all_goods():        print(all_goods.get(good_name))    else:        print('商品不存在')def delete_good():  #删除    good_name = input('请输入商品名称:').strip()    if good_name in all_goods:        all_goods.pop(good_name)        op_file(FILE_NAME,all_goods)    else:        print('商品不存在')def main():    for i in range(3):        choice = input('请输入你的选择'                   '1.添加'                   '2.修改'                   '3.删除'                   '4.查看'                   '5.退出')        if choice =='1':            add_good()        elif choice =='2':            update_good()        elif choice =='3':            delete_good()        elif choice =='4':            query_good()        elif choice =='5':            quit('程序退出')        else:            print('输入错误,请重新输入!')            return main()main()

 

转载于:https://www.cnblogs.com/nuobao/p/9100900.html

你可能感兴趣的文章
rsync工作方式介绍03
查看>>
Lync 小技巧-38-Lync Server 2013与Exchange Server高可用环境-集成
查看>>
制作Windows与ESXi的系统安装工具U盘
查看>>
美国高校学生如何使用计算机实验室
查看>>
以太坊Crypto Countries加密国家火爆,区块链游戏成智能合约应用探索突破口
查看>>
ansible的playbook配置及template模板的使用
查看>>
红帽:加速OpenStack商用化进程
查看>>
HDU 1085 Holding Bin-Laden Captive!
查看>>
图片批量缩略图组件(dll),支持asp,asp.net,vb,vb.net等调用
查看>>
2011年7月29日星期五
查看>>
厚积薄发,丰富的公用类库积累,助你高效进行系统开发(4)----CSV、Excel、INI文件、独立存储等文件相关...
查看>>
求一个字符串中连续出现次数最多的子串
查看>>
web.py大文件下载
查看>>
ServU:无法访问servu服务器
查看>>
解析SNS社区产品架构模型,互联网营销
查看>>
perl处理命令行参数
查看>>
.NET Remoting过时了吗?为什么公司的项目还是选择用.NET Remoting,而不是WCF?
查看>>
Recovery中英文对照
查看>>
正则表达式从入门到精通
查看>>
Haproxy使用总结
查看>>