商品管理小程序

时间:2019-09-27
本文章向大家介绍商品管理小程序,主要包括商品管理小程序使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1、写一个商品管理的小程序(包含字典操作+文件操作+函数)

2、商品存在文件里面


1、添加商品
输入产品名称、颜色、价格
要校验商品是否存在,价格是否合法
输入是否为空
2、修改商品信息
输入产品名称、颜色、价格
要校验商品是否存在,价格是否合法
输入是否为空
3、查看商品信息
输入商品名称
输入all就展示全部商品
输入的是其他的商品名称,
4、删除商品
输入商品名称

import json
file_name = 'product.json'
f = open('product.json', encoding='utf-8')
product_info = json.load(f)

#校验产品名称是否存在的函数
def check_name(pro_name):
    names = []
    for k in product_info:
        names.append(k)
    if pro_name in names:
        return True
    return False

#校验输入的价格是否为正数的函数
def is_price(pro_price):
    pro_price = str(pro_price)
    if pro_price.isdigit() :  #判断价格是否为正整数
        return True
    elif pro_price.count('.')==1 :    #判断价格是否为正小数
        left,right = pro_price.split('.')
        if left.isdigit() and right.isdigit():
            return True
        return False
    return False

#添加商品的函数
def add_product(pro_name):
    if check_name(pro_name):    #调用检查商品是否存在的函数
        print('该商品已存在,不能重复添加')
    else:
        add_info={}
        pro_color = input('请输入商品颜色:')  #填写新增商品信息
        pro_price = input('请输入商品价格:')
        if not pro_color.strip() or not pro_price.strip():
            print('输入不能为空')
        elif not is_price(pro_price):
            print('商品价格需要输入正数')
        else:
            add_info['color']=pro_color
            add_info['price']=float(pro_price)
            product_info[pro_name]=add_info
            product_json = json.dumps(product_info,ensure_ascii=False,indent=8)
            f1 = open('product.json','w',encoding='utf-8')
            f1.write(product_json)
            f1.close()
            print('添加商品成功')

#展示商品的函数
def show_product(pro_name):
    if pro_name == 'all':
        print(product_info)
    elif  not check_name(pro_name):
        print('商品不存在')
    else:
        print(pro_name,product_info[pro_name])

#删除商品的函数
def del_product(pro_name):
    if not check_name(pro_name):
        print('商品不存在')
    else:
        product_info.pop(pro_name)
        product_json = json.dumps(product_info,ensure_ascii=False,indent=8)
        with open('product.json','w',encoding='utf-8') as f2:
            f2.write(product_json)
        print('已删除“%s”的商品信息'%pro_name)

#更新商品的函数
def update_product(pro_name):
    if not check_name(pro_name):
        print('商品不存在')
    else:
        update_info = {}
        pro_color = input('请输入商品的颜色:')
        pro_price = input('请输入商品的价格:')
        if not pro_color.strip() or not pro_price.strip():
            print('输入不能为空')
        elif not is_price(pro_price):
            print('商品价格需要输入正数')
        else:
            update_info['color'] = pro_color
            update_info['price'] = float(pro_price)
            product_info[pro_name] = update_info
            product_json = json.dumps(product_info,ensure_ascii=False,indent=8)
            with open('product.json','w',encoding='utf-8') as f3:
                f3.write(product_json)
            print('修改“%s”的商品信息成功'%pro_name)

choice = input('1.添加商品2.修改商品3.删除商品4,展示商品信息\n请输入:')
if not choice.strip():
    print('输入不能为空')
elif choice!='1' and choice!='2' and choice!='3' and choice!='4':
    print('非法输入')
else:
    name = input('请输入要添加的商品名称:')
    if not name.strip():
        print('商品名称不能为空')
    else:
        if choice=='1':
            add_product(name)
        elif choice=='2':
            update_product(name)
        elif choice=='3':
            del_product(name)
        elif choice=='4':
            show_product(name)

原文地址:https://www.cnblogs.com/cathyg/p/11597578.html