商品管理的程序,商品信息都存在一个json串里面
1、查询商品信息 #校验商品是否存在
2、新增商品 # #校验商品是否存在 #校验价格是否合法
3、修改商品信息 ##校验商品是否存在
if chice =="1":
query_goods()
elif choice = ="2":
add_goods()

goods.json:

{
"iphonex":{
"num":100,
"price":1999.98,
"color":"red"
},
"car":{
"num":100,
"price":9999999,
"color":"black"
}
}

import json
FILE_NAME="goods.json"
def check_goods(name,newGood=None):
    with open(FILE_NAME, "r+", encoding="utf-8") as f:
        goods_dic = json.load(f)
    if newGood:
        goods_dic[name]=newGood.get(name) #don't use setdefault function, because cann't update the origibal valye
       
print(goods_dic)
        with open(FILE_NAME,"w+",encoding="utf-8") as fw:
            json.dump(goods_dic,fw,ensure_ascii=False,indent=4)
    else:
        #with open(FILE_NAME,"r+",encoding="utf-8") as f:
            #goods_dic=json.load(f)
       
if goods_dic.get(name):
            print(name + " 存在!")
            return goods_dic
        else:
            print(name +" 不存在!")
            return False

def check_price(price):
    if price.count(".")==0 and price.isdigit() and int(price)>=0:
        print("price is correct int!")
        return True
    elif price.count(".")==1:
        list=price.split(".")
        left = list[0]
        right=list[1]
        if left.isdigit() and right.isdigit():
            print("price is correct float!")
            return True
    else:
        print("price is illegal!")
        return False

choice = input("please input your choice:"
               " 1. check goods info."
               " 2. add goods info"
               " 3. edit goods info")
if choice =='1':
    goods_name = input("please input the goods name:").strip()
    good_dis=check_goods(goods_name)
    if good_dis:
        print(good_dis.get(goods_name))
elif choice=='2':
    name_add=input("please input the goods name you want to add:").strip()
    if check_goods(name_add):
        print("this goods name exist!")
    else:
        good_add_dic={}
        num = int(input("please input the number:"))
        color=input("please input the color:")
        price = input("please input the price:").strip()
        if check_price(price):
            info_add = {"num": num, "price": price, "color:": color}
            good_add_dic.setdefault(name_add,info_add)
            print("成功添加商品!商品信息为:"+str(good_add_dic))
            check_goods(name_add,good_add_dic)
        else:
            print("价格输入不正确!")
elif choice =="3":
    name_edit=input("please input the goods name you want to edit:")
    if check_goods(name_edit):
        goods_edit_dic=check_goods(name_edit) # get the goods dic
       
good_info=goods_edit_dic.get(name_edit) # get the edited good info
       
changeInfo=input("please choose the info you want to change:"
                         "1. num"
                         "2. color"
                         "3. price").strip()
        if changeInfo =='1':
            num1 = int(input("please input the number:"))
            good_info["num"]=num1
        if changeInfo =='2':
            color1 = input("please input the color:")
            good_info["color"]=color1
        if changeInfo =='3':
            price1 = input("please input the price:")
            if check_price(price1):
                good_info["price"]=price1
        goods_edit_dic[name_edit]=good_info # update the edited good info into the dic
       
print(str(goods_edit_dic))
        check_goods(name_edit,goods_edit_dic)
    else:
        print("该商品不存在,无法编辑修改")
else:
    print("输入不正确")

运行结果:

总结:

1. 首先定义两个函数,一个用于检查商品是否存在,一个用于验证价格是否有效

2. 在函数check_goods中,若是需要添加或更新商品时,在更新字典时,需要使用键值对更新的方式,不能用setdefault函数,因为 setdefault在key存在时,不会更新value。如果字典中包含有给定键,则返回该键对应的值,否则返回为该键设置的值。在调试中最开始用的 setdefault方法,最后发现没有更新到新的值。

goods_dic[name]=newGood.get(name) #don't use setdefault function, because cann't update the origibal valye

3. 在函数check_goods中,查询时若有该商品时,返回该商品的信息,即返 goods_dic.get(name),这样在查询时可以直接输出该商品信息,但后面在调试中,发现在新增或更新商品时,也需要返回所有商品信息的字典,所以后来改为返回整个商品的字典,这样可以方便这个函数最大化的使用。

if goods_dic.get(name):
    print(name + " 存在!")
    return goods_dic

4. 在check_price函数中,最开始想使用type() 方法判断输入的类型 int,float,str来判断是否输入有效,在单独的函数调用时,可以输入int,float,可以使用该函数,但是后期调用是要通过键盘输入的方式取得参数值price,而该值类型为str, 所以在调用函数时总是显示price is illegal.  所以调试后重新修改该函数。 在编写时需要注意参数传递的方式。

python3 - 商品管理的程序,商品信息都存在一个json串里面的更多相关文章

  1. Python3.x:访问带参数链接并且获取返回json串

    Python3.x:访问带参数链接并且获取返回json串 示例一: import json import xml.dom.minidom from urllib import request, par ...

  2. SQL Server 【附】创建"商品管理数据库"、"学生选课数据库"的SQL语句

    附:(创建“商品管理数据库”的SQL语句) --建立"商品管理数据库"数据库-- create database 商品管理数据库 on(name='商品管理数据库_m', file ...

  3. Vue小案例 之 商品管理------学习过滤器 使用过滤器处理日期的格式

    代码学习过滤器 过滤器介绍:过滤模型数据,在数据显示前做预处理操作: 内置过滤器:在1.x中,Vue提供了内置过滤器,但是在2.x中已经完全废除: 解决办法: (1)使用第三方库来替代1.x中的内置过 ...

  4. python作业/练习/实战:3、实现商品管理的一个程序

    作业要求 实现一个商品管理的一个程序,运行程序有三个选项,输入1添加商品:输入2删除商品:输入3 查看商品信息1.添加商品: 商品名称:xx 商品如果已经存在,提示商品已存在 商品价格:xx数量只能为 ...

  5. [毕业设计][期末作业]二手闲置小程序 免费信息发布系统功能源码(小程序+php后台管理)

    最近做了一个小程序,主要是二手闲置免费信息发布系统的功能,里面包括了登录,发布商品,商品管理,违规投诉,canva商品海报生成,分享等一些基础的功能,可以说代码都是自己辛辛苦苦写出来的.可作为毕业设计 ...

  6. C#开发微信门户及应用(23)-微信小店商品管理接口的封装和测试

    在上篇<C#开发微信门户及应用(22)-微信小店的开发和使用>里面介绍了一些微信小店的基础知识,以及对应的对象模型,本篇继续微信小店的主题,介绍其中API接口的封装和测试使用.微信小店的相 ...

  7. python之商品操作小程序

    要求:写一个添加商品的程序,商品信息写入txt文件中,以二维字典形式比如:{‘小米’:{‘价格’:‘1999元’,‘数量’:10}} 1.添加商品 #商品名称 #价格 #数量 2.查看商品 3.删除商 ...

  8. Day09_商品管理

    学于黑马和传智播客联合做的教学项目 感谢 黑马官网 传智播客官网 微信搜索"艺术行者",关注并回复关键词"乐优商城"获取视频和教程资料! b站在线视频 0.学习 ...

  9. 「超市管理系统——商品管理」 · Java Swing + MySQL JDBC开发

    项目下载:https://download.csdn.net/download/weixin_44893902/13715024 1.9元付费赞助下载:https://download.csdn.ne ...

随机推荐

  1. MVC总结--MVC简单介绍以及和WebForm差别

    什么是MVC MVC(Model-View-Controller.模型-视图-控制器模式)用于表示一种软件架构模式.它把软件系统分为三个基本部分:模型(Model),视图(View)和控制器(Cont ...

  2. BZOJ 2005 NOI2010 能量採集 数论+容斥原理

    题目大意:给定n和m.求Σ(1<=i<=n)Σ(1<=j<=m)GCD(i,j)*2-1 i和j的限制不同,传统的线性筛法失效了.这里我们考虑容斥原理 令f[x]为GCD(i, ...

  3. 如何创建RESTFul Web服务

    想写这篇文章很久了,这是个大话题,不是一时半会就能说清楚的. 所以准备花个一星期整理资料,把思路理清楚,然后再在Team里做个sharing:) 其实RESTFul是架构风格,并不是实现规范,也不一定 ...

  4. Django之站内搜索-Solr,Haystack

    java -version 不多说 solr 是java 开发的 java version "1.7.0_79" Java(TM) SE Runtime Environment ( ...

  5. 在eclipse中添加android ADT

    对于程序开发的学者来说,eclipse并不陌生,它为我们提供了一个非常广阔的平台来开发程序.同样我们也可以用它来开发android程序. 但是在eclipse中并不能直接开发android程序,需要我 ...

  6. eclipse-jee版配置tomcat

    Eclipse作为一款优秀的java开发开源IDE,集成了许多优秀的开发控件.下来我就如何安装eclipse及插件进行说明: 一.JDK安装   JDK是作为整个java的核心,包括运行环境,编译工具 ...

  7. android WebView详细使用方法(转)

    1.最全面的Android Webview详解 2.最全面总结 Android WebView与 JS 的交互方式 3.你不知道的 Android WebView 使用漏洞 如果想保证登录状态,就插入 ...

  8. 将參数从PHP传递到JavaScript中

    php: //自己定义数组參数 $newarr = array('a1' => 'a1', 'a2' => 'a2', 'a3' => 'a3'); $config = CJavaS ...

  9. NSSrting的几种经常使用的使用方法

    1.创建NSString字符串 NSString 与 char* 最大的差别就是 NSString是一个objective对象,而char* 是一个字节数组. @+" 字符串 " ...

  10. VMware unrecoverable error解决方法

    把开发环境部署在虚拟机里面,重装系统后不须要再反复部署开发环境. 可是有时候异常退出虚拟机会导致错误.之前出现打开虚拟机之后,系统分辨率错误,就是点击的位置和显示的位置不一样. 于是又一次关了虚拟机, ...