python字典操作+文件操作+函数


需求:

# 1、写一个商品管理的小程序
# 2、商品存在文件里面
# 1、添加商品
# 输入产品名称、颜色、价格
# 要校验商品是否存在,价格是否合法
# 输入是否为空
# 2、修改商品信息
# 输入产品名称、颜色、价格
# 要校验商品是否存在,价格是否合法
# 输入是否为空
# 3、查看商品信息
# 输入商品名称
# 输入all就展示全部商品
# 输入的是其他的商品名称,
# 4、删除商品
# 输入商品名称

如果需要运行下列代码,请先在代码同级目录创建一个shangpin.txt文件,并写入一些内容,目前目录为空还没有做判断,如果为空会报错,内容例如:
{
    "iphone": {
        "color": "高亮黑",
        "price": "99.99"
    },
    "电视": {
        "color": "红色",
        "price": "99.99"
    },
    "冰箱": {
        "color": "白色",
        "price": "99.99"
    }
}

代码如下:

import json

#读取商品信息
def goods():
    with open('shangpin.txt', encoding='utf-8') as f:
        str = json.load(f)
    return str

#判断商品是否存在
def is_good(name):
    if name in goods():
        return False
    else:
        return True

#判断价格的正确性
def is_price(price):
    price = price.strip()
    if price and price.count('.') == 1:
        left,right = price.split('.')
        if left.isdigit() and right.isdigit() and int(right) < 100 and float(price) > 0:
            return True
        else:
            return False
    elif price.isdigit() and int(price) > 0:
        return True
    else:
        return False

#添加或修改商品信息
def updata_add(name,color,price):
    all_goods = goods()
    all_goods[name] = {'color': color, 'price': price}
    with open('shangpin.txt', 'w', encoding='utf-8') as f:
        json.dump(all_goods, f, ensure_ascii=False, indent=4)

#添加商品
def inset_goods():
    name = (input('请输入商品名称:')).strip()
    color = (input('请输入颜色:')).strip()
    price = (input('请输入价格:')).strip()
    if name and color and price and is_good(name) and is_price(price):
        updata_add(name,color,price)
        print('添加成功!')
    elif name and is_good(name) is False:
        print('商品已存在!')
    else:
        print('输入错误!')

#修改商品
def updata_goods():
    name = (input('请输入商品名称:')).strip()
    color = (input('请输入颜色:')).strip()
    price = (input('请输入价格:')).strip()
    if name and color and price and is_good(name) is False and is_price(price):
        updata_add(name,color,price)
        print('修改成功!')
    elif name and is_good(name):
        print('商品不存在!')
    else:
        print('输入错误')

#查看商品信息
def see_goods():
    goods_name = input('输入具体商品名称,要查看所有输入all:')
    if goods_name.strip() != '':
        if goods_name == 'all':
            print(goods())
        elif goods_name in goods():
            print(goods_name,goods()[goods_name])
        else:
            print('没有这个商品!')
    else:
        print('输入错误!')

#删除商品
def del_goods():
    goods_name = (input('请输入商品名称:')).strip()
    if goods_name.strip() != '':
        if goods_name in goods():
            all_goods = goods()
            del all_goods[goods_name]
            with open('shangpin.txt', 'w', encoding='utf-8') as f:
                json.dump(all_goods, f, ensure_ascii=False, indent=4)
                print('删除成功!')
        else:
            print('没有这个商品!')
    else:
        print('输入错误!')

while True:
    check = (input('请输入数字,1表示查询、2表示新增、3表示修改、4表示删除:')).strip()
    if check.isdigit():
        check = int(check)
        if check == 1:
            see_goods()
        elif check == 2:
            inset_goods()
            print(goods())
        elif check ==3:
            updata_goods()
            print(goods())
        elif check == 4:
            del_goods()
            print(goods())
        else:
            print('输入错误请重新输入!')
    else:
        print('输入非数字!')

 

posted @ 2019-10-15 20:24  会武术的跳跳虎  阅读(320)  评论(0编辑  收藏  举报