zoukankan      html  css  js  c++  java
  • 函数的商城代码练习

    本周作业:
    写一个管理商品的程序,商品文件格式在a.json里面
    提供商品的增删改查功能
    choice = input('请输入你的选择:1、查看商品 2、新增商品 3、修改商品 4、删除商品')
    #1、查看商品,输入商品名称,print单个商品的信息,价格、数量,输入all,查看所有商品
    #2、新增商品,输入商品名称、数量、价格,数量是大于0的整数,价格必须是大于0的数值,
    #如果商品存在,无法添加
    #3、修改商品,输入商品名称、数量、价格,商品存在才可以修改 数量是大于0的整数,价格必须是大于0的数值,
    #4、输入商品名称,如果存在,删除
     
     
    分析流程图
     
    我自己写的
    import json
    #小数这块调用 封装写的调用
    file='a.json'#定义常量
    def ces(file,content=None):
    if content:
    with open('a.json', 'a+', encoding='utf-8') as fw:
    fw.seek(0)
    json.dump(content, fw, ensure_ascii=False, indent=4)
    else:
    with open(file,encoding='utf-8') as fr:
    d = json.load(fr)
    return d
    def cha(): #查找
    dics=ces(file)
    print(dics)
    def is_float(s):
    s = str(s)
    if s.count('.') == 1: #1.3
    left,right = s.split('.')
    if left.isdigit() and right.isdigit():#正小数
    return True
    return False
    def add():
    dics = ces(file)
    name=input('输入商品名称').strip()
    price=input('输入商品价格').strip()
    count=input('输入商品数量 ').strip()
    if not name and not price and not count:
    print('不允许输入为空')
    elif is_float(price) or is_float(count):
    print('输入不合法')
    elif name in dics:
    print('商品已经存在')
    else:
    dics[name]={"count":count,"price":price}
    print(dics)
    def delete():
    dics = ces(file)
    name=input('输入商品名称').strip()
    if name in dics:
    del dics[name]
    def update():
    dics = ces(file)
    name = input('输入商品名称').strip()
    price = input('输入商品价格').strip()
    count = input('输入商品数量 ').strip()
    if name in dics:
    cps=input('输入修改字体')
    dics[name]=cps
    else:
    print('商品不存在')

    choice = input('请输入你的选择:1、查看商品 2、新增商品 3、修改商品 4、删除商品')
    if choice=='1':
    add()


    优化后的代码注意有
    def is_price(price):  #>0的整数和小数都可以 #1.7
    s = str(price)
    if is_digit(s):#先判断是不是整数
    return True
    else:#小数判断
    if s.count('.') == 1: # 1.3
    left, right = s.split('.')
    if left.isdigit() and right.isdigit(): # 正小数 #0.0
    if float(s)>0:#小数大于0
    return True 这个没有写出来

    def is_digit(number):                                                       这个count判断正整数没有写出来
    s = str(number)
    if s.isdigit():
    if int(s) > 0:
    return True

    def delete_product():
    product_name = input('请输入商品名称:').strip()
    if product_name:
    products = read_products()
    if product_name not in products:
    print('商品不存在')
    else:
    products.pop(product_name)
    write_products(products) 删除这里应该在写一次这里没有注意
     
    func_map = {'1':show_product,'2':add_product,'3':modify_product,'4':delete_product}     这里变成一个字典很牛比
    if choice in func_map:
    func_map[choice]()
    else:
    print('请输入正确的选项!')


    优化后的代码

    import json

    FILE_NAME = 'a.json' #常量
    def read_products(): #把读文件转成字典定义一个函数
    with open(FILE_NAME,encoding='utf-8') as fr:
    return json.load(fr)

    def write_products(data): #写的内容 封装一个函数
    with open(FILE_NAME,'w',encoding='utf-8') as fw:
    json.dump(data,fw,ensure_ascii=False,indent=4)

    def is_digit(number):
    s = str(number)
    if s.isdigit():
    if int(s) > 0:
    return True

    def is_price(price): #>0的整数和小数都可以 #1.7
    s = str(price)
    if is_digit(s):#先判断是不是整数
    return True
    else:#小数判断
    if s.count('.') == 1: # 1.3
    left, right = s.split('.')
    if left.isdigit() and right.isdigit(): # 正小数 #0.0
    if float(s)>0:#小数大于0
    return True

    def show_product(): #读的函数
    product_name = input('请输入商品名称:').strip()
    if product_name:
    products = read_products()
    if product_name == 'all':
    print(products)
    elif product_name not in products:
    print('商品不存在')
    else:
    product = products.get(product_name)
    print('商品信息:', product)
    else:
    print('不能为空')

    def add_product():
    product_name = input('请输入商品名称:').strip()
    price = input('请输入商品价格:').strip()
    count = input('请输入商品数量:').strip()
    if product_name and price and count:
    if is_price(price) and is_digit(count):
    products = read_products()
    if product_name not in products:
    products[product_name] = {"count":count,"price":price}
    write_products(products)
    print('商品新增成功!')
    else:
    print('商品已经存在')
    else:
    print('价格/数量不合法')
    else:
    print('不能为空')


    def modify_product():
    product_name = input('请输入商品名称:').strip()
    # new_product_name = input('请输入新的商品名称:').strip()
    price = input('请输入商品价格:').strip()
    count = input('请输入商品数量:').strip()
    if product_name and price and count:
    if is_price(price) and is_digit(count):
    products = read_products()
    if product_name in products:
    # products.pop(product_name)
    products[product_name] = {"count":count,"price":price}
    # products[new_product_name] = {"count":count,"price":price}
    write_products(products)
    print('商品修改成功!')
    else:
    print('商品不存在')
    else:
    print('价格/数量不合法')
    else:
    print('不能为空')


    def delete_product():
    product_name = input('请输入商品名称:').strip()
    if product_name:
    products = read_products()
    if product_name not in products:
    print('商品不存在')
    else:
    products.pop(product_name)
    write_products(products)

    else:
    print('不能为空')


    choice = input('1、查看商品 2、新增 3、修改 4、删除: ')

    func_map = {'1':show_product,'2':add_product,'3':modify_product,'4':delete_product}
    if choice in func_map:
    func_map[choice]()
    else:
    print('请输入正确的选项!')

    # if choice == '1':
    # show_product()
    # elif choice == '2':
    # add_product()



  • 相关阅读:
    允许使用root远程ssh登录(Ubuntu 16.04)
    Nginx反向代理,负载均衡,redis session共享,keepalived高可用
    Spring MVC框架:第十六章:细节了解
    Spring MVC框架:第十五章:多IOC容器整合
    Spring MVC框架:第十四章:数据校验
    Spring MVC框架:第十三章:类型转换
    Spring MVC框架:第十二章:运行原理
    Spring MVC框架:第十一章:Ajax
    Spring MVC框架:第九章:文件上传
    Spring MVC框架:第十章:拦截器
  • 原文地址:https://www.cnblogs.com/weilemeizi/p/14507059.html
Copyright © 2011-2022 走看看