管理商品demo
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的更多相关文章
- 基于ZKWeb + Angular 4.0的开源管理后台Demo
这是一套基于ZKWeb网页框架和Angular 4.0编写的开源管理后台Demo,实现了前后端分离和模块化开发, 地址是: https://github.com/zkweb-framework/ZKW ...
- 微信公众号开发系统入门教程(公众号注册、开发环境搭建、access_token管理、Demo实现、natapp外网穿透)
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/a1786223749/article/ ...
- 1.0 多控制器管理(附:Demo)
本文并非最终版本,如有更新或更正会第一时间置顶,联系方式详见文末 如果觉得本文内容过长,请前往本人 “简书” 控制器 : 一个iOS的app很少只由一个控制器组成,除非这个app极其简 ...
- C# Process类_进程管理器Demo
Process用于管理计算机的进程,下面给出一个C#进程管理器的DEMO. namespace ProcessManager { public partial class Form1 : Form { ...
- 【vue】MongoDB+Nodejs+express+Vue后台管理项目Demo
¶项目分析 一个完整的网站服务架构,包括: 1.web frame ---这里应用express框架 2.web server ---这里应用nodejs 3.Database ---这里 ...
- Spring事务管理的demo
事务是逻辑上的一组操作,这组操作要么全部成功,要么全部失败,最为典型的就是银行转账的案例: A要向B转账,现在A,B各自账户中有1000元,A要给B转200元,那么这个转账就必须保证是一个事务,防止中 ...
- PHP.38-TP框架商城应用实例-后台14-商品管理-商品扩展分类的删除、修改
商品分类删除 1.删除商品时,根据商品id删除扩展分类表数据 商品扩展分类修改 1.在控制器GoodsController.class.php/edit()中根据商品id取出对应的所有扩展分类 2.在 ...
- 使用java自带线程池管理器demo
1.程序入口:DabianTest package com.lbh.myThreadPool.present; import java.util.concurrent.ExecutorService; ...
- 三级设置页面管理测试demo
#include "PhoneBookWindow.h"#include "xWindow/xWindow.h"#include "hardwareD ...
随机推荐
- 解决CUDA driver version is insufficient for CUDA runtime version
问题 在服务器上安装mxne的GPU版本 sudo pip install mxnet-cu80==1.2.1 然后在gpu上创建数据 import mxnet as mx mx.nd.array([ ...
- dva构建react项目
第一步:安装 dva-cli 1 cnpm install dva-cli -g 第二步:采用dva来创建项目: 1 dva new react_two 2 cd react_two 用webstor ...
- Linux 创建自定义命令
Linux 创建自定义命令 Linux 可以创建自定义使用命令 这里我们采取使用“alias”命令.这里我们首先了解两个文件,通过这两个文件我们可以根据环境配置相应的自定义命令. 该文件内创建的自定义 ...
- linux 英汉词典程序shell+postgresql版
在linux控制台下工作,有时候遇到不懂的单词,想要找个linux下的词典程序,搜寻无果,只好自己动手写个了. 首先获取词典文本文件,在github上找到一个 建立数据库 create databas ...
- bzoj2091: [Poi2010]The Minima Game DP
2091: [Poi2010]The Minima Game DP 链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2091 思路 这类问题好迷 ...
- centos 7 已经开启 22 端口但无法连接
已经开启 22 端口但无法连接 刚买的 vps ,默认 ssh 端口是 29488, 使用以下方式连接ssh -p 29488 root@x.x.x.x觉得加端口有点麻烦, 希望使用默认的 22 端口 ...
- Mac cnpm安装失败及解决方案
首先安装node 官网下载安装包,傻瓜式安装:https://nodejs.org/zh-cn/ 淘宝镜像安装cnpm, 在终端输入: npm install -g cnpm --registry=h ...
- Linux常用命令——文件搜索命令
Linux常用命令——文件搜索命令 Linux 以#号开头的内容都是内容描述或配置项 find 描述:文件搜索 语法:find [搜索范围] [范围条件] . 当前目录 示例:[root@local ...
- Intellij IDEA创建git,maven的SpringMVC项目
Intellij IDEA创建git,maven的SpringMVC项目 原文链接:http://www.cnblogs.com/blog5277/p/8906120.html 原文作者:博客园--曲 ...
- robot framework学习二-----元素定位
文章摘自:https://www.cnblogs.com/fnng/p/3901391.html 不要误认为Robot framework 只是个web UI测试工具,更正确的理解Robot fram ...