1、写一个管理商品的程序
# 1、商品存在文件里面
# 2、添加商品的时候,商品存在的就不能添加了,数量只能是大于0的整数,价格可以是小数、整数,但是只能是大于0的
# 商品名称
# 商品价格
# 商品数量
# 3、删除的商品的时候输入商品名称,商品不存在,要提示
# 4、修改商品的时候
# 商品名称
# 商品价格
# 商品数量
# 5、查看商品,输入商品名称,查询到商品的信息
# choice = input('请输入你的选择:'
# '1、添加商品'
# '2、删除商品'
# '3、修改商品信息'
# '4、查看商品'
# '5、退出'
import json
FILENAME='Product.json'
def read_file():
with open(FILENAME,encoding='utf-8')as fw:
return json.load(fw)
def write_file(d):
with open(FILENAME,'w',encoding='utf-8')as fw:
return json.dump(d,fw,indent=4,ensure_ascii=False)
def check_int(num):
num=str(num)
if num.isdigit() and int(num)>0:
return True
def check_price(num):
num=str(num)
if num.count('.')==1:
left,right=num.split('.')
if left.isdigit() and right.isdigit() and int(right)>0:
return True
elif check_int(num):
return True
all_product=read_file()
def add_product():
name=input('输入商品名称:').strip()
count=input('请输入数量:').strip()
price=input('请输入价格').strip()
if name and count and price:
if not check_int(count):
print('请输入正确的数量')
elif not check_price(price):
print('请输入正确的价格')
elif name in all_product:
print('商品已存在')
else:
all_product[name]={'price':price,'count':count}
write_file(all_product)
print('添加成功')
else:
print('不能为空')
def edit_product():
name=input('输入名称').strip()
while True:
if name in all_product:
break
elif name=='q':
quit('退出')
count= input('输入数量').strip()
price= input('输入价格').strip()
if name and count and price:
all_product[name] = {'price': price, 'count': count}
write_file(all_product)
print('修改成功')
else:
print('不能为空')
def del_product():
name=input('商品名称').strip()
if name not in all_product:
print('商品不存在')
else:
all_product.pop(name)
write_file(all_product)
print('删除成功')
def show_product():
while True:
name = input('商品名称').strip()
if name in all_product:
print(all_product.get(name))
break
elif name=='all':
print(json.dumps(all_product,indent=4,ensure_ascii=False)) func_map={
'1':add_product,
'2':del_product,
'3':edit_product,
'4':show_product
}
for i in range(4):
choice=input('输入你的选择\n:'
'1添加\n'
'2删除\n'
'3修改\n'
'4查看\n'
)
if choice in func_map:
func_map[choice]()
else:
print('请输入正确选项')

管理商品demo的更多相关文章

  1. 基于ZKWeb + Angular 4.0的开源管理后台Demo

    这是一套基于ZKWeb网页框架和Angular 4.0编写的开源管理后台Demo,实现了前后端分离和模块化开发, 地址是: https://github.com/zkweb-framework/ZKW ...

  2. 微信公众号开发系统入门教程(公众号注册、开发环境搭建、access_token管理、Demo实现、natapp外网穿透)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/a1786223749/article/ ...

  3. 1.0 多控制器管理(附:Demo)

    本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书”       控制器 :   一个iOS的app很少只由一个控制器组成,除非这个app极其简 ...

  4. C# Process类_进程管理器Demo

    Process用于管理计算机的进程,下面给出一个C#进程管理器的DEMO. namespace ProcessManager { public partial class Form1 : Form { ...

  5. 【vue】MongoDB+Nodejs+express+Vue后台管理项目Demo

    ¶项目分析 一个完整的网站服务架构,包括:   1.web frame ---这里应用express框架   2.web server ---这里应用nodejs   3.Database ---这里 ...

  6. Spring事务管理的demo

    事务是逻辑上的一组操作,这组操作要么全部成功,要么全部失败,最为典型的就是银行转账的案例: A要向B转账,现在A,B各自账户中有1000元,A要给B转200元,那么这个转账就必须保证是一个事务,防止中 ...

  7. PHP.38-TP框架商城应用实例-后台14-商品管理-商品扩展分类的删除、修改

    商品分类删除 1.删除商品时,根据商品id删除扩展分类表数据 商品扩展分类修改 1.在控制器GoodsController.class.php/edit()中根据商品id取出对应的所有扩展分类 2.在 ...

  8. 使用java自带线程池管理器demo

    1.程序入口:DabianTest package com.lbh.myThreadPool.present; import java.util.concurrent.ExecutorService; ...

  9. 三级设置页面管理测试demo

    #include "PhoneBookWindow.h"#include "xWindow/xWindow.h"#include "hardwareD ...

随机推荐

  1. vue-awesome-swiper组件不能自动播放和导航器小圆点不显示问题

    from: https://blog.csdn.net/osdfhv/article/details/79062427 <template> <div class="swi ...

  2. Linux Shell自动交互/人机交互的三种方法

    如果你要学习linux,你可能会遇到Linux Shell自动交互问题,这里将介绍Linux Shell自动交互的解决方法,在这里拿出来和大家分享一下. 一.背景 shell脚本在处理自动循环或大的任 ...

  3. 校验input 修改当前值的校验获取值方式

    <input style="height: 18px; width: 90px;" name="nowQty" value="${ad.nowQ ...

  4. js 星星效果思路

    //星星的效果思路 1.获取需要修改的元素 ul li 跟p 布局 2.给li 加移入事件 更改提示框显示, 3.给li 加移出事件 更改提示框隐藏 4.给li加索引值代表自己的序号 5.在li移入时 ...

  5. 关于使用MUI框架ashx获取值的问题

    前台如有 var value = '<%= value%>';  后台在使用  Params["value"]的时候会出现重复(例:value,value).

  6. web前端开发常用组件

    web前端开发常用组件 1. 对话框(dialog):jbox(适合对话框等其它功能).colorbox(也很强大,可以弥补jbox图片轮播的落点),      这二者基本能搞定所有对话框的情况 2. ...

  7. win的使用

    1.net use 挂在远端文件系统,方便本机软件操作! 添加挂载文件   net use  h:   //192.168.1.1/file 删除挂载文件   net use /delete  h: ...

  8. lmbench用于arm测试

    一.下载 http://www.bitmover.com/lmbench/lmbench.html网站有lmbench的介绍 下载地址如下(lmbench3): http://www.bitmover ...

  9. Guitar Pro里的编谱方式怎么用?

    今天来教大家如何使用Guitar Pro中的编谱方式来让我们的乐谱更加美观整齐耐看,我们一起get起来吧! 相信我们每一个人在使用Guitar Pro进行编曲创作时,都会碰到这种情况,在乐谱上,会看到 ...

  10. 【jvisualvm远程监控配置】

    第一步:首先通过hostname -i  和ifconfig命令,查看一下IP地址是否一致 如果不一致,修改hosts的IP地址:vi /etc/hosts    把ip地址和ifconfig的地址修 ...