管理商品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 ...
随机推荐
- selenium.common.exceptions.WebDriverException: Message: unknown Error: cannot find Chrome binary
使用Chrome浏览器时,经常会遇到以下报错:浏览器没有调用起来 selenium.common.exceptions.WebDriverException: Message: unknown Err ...
- 数据库SQL的多表查询
数据库 SQL 的多表查询:eg: table1: employees, table2: departments,table3: salary_grades; 一:内连接: 1):等值连接: 把表em ...
- linux iptables详解(转)
概述 netfilter/iptables(简称为iptables)组成Linux平台下的包过滤防火墙,与大多数的Linux软件一样,这个包过滤防火墙是免费的,它可以代替昂贵的商业防火墙解决方案,完成 ...
- Delphi文件目录操作(转载)
Delphi判断文件夹(目录)是否存在,不存在就创建一个,可以一次创建多级目录 procedure TForm1.Button1Click(Sender: TObject);begin if not ...
- configparser配置文件处理
创建一个configparser格式的文档: import configparser config = configparser.ConfigParser()config["DEFAULT& ...
- winform中的dateTimePicker控件设置默认值为空
winform中的dateTimePicker控件设置默认值为空 第一步:设置Format的属性值为“Custom” 第二步:设置CustomFormat的属性值为空,需要按一个空格键
- linux进程虚拟地址空间
转载源 在多任务操作系统中,每个进程都运行在属于自己的内存沙盘中.这个沙盘就是虚拟地址空间(Virtual Address Space),在32位模式下它是一个4GB的内存地址块.在Linux系统中, ...
- P1636 Einstein学画画
一笔画问题 P1636 Einstein学画画 如果一个图存在一笔画,则一笔画的路径叫做欧拉路,如果最后又回到起点,那这个路径叫做欧拉回路. 奇点:跟这个点相邻的边数目有奇数个的点 不存在奇数个奇点的 ...
- 第六节 DOM操作应用
创建.插入和删除元素 创建DOM元素: createElement(标签名); //创建一个节点 appendChild(节点); //追加一个节点 例如:document.body.appendCh ...
- python from entry to abandon3
第十章的内容是解决问题————编写一个Python脚本.在我的电脑上因为Zip命令不能正常工作所以无法给出演示.该章给出了很有意义的编程思路,对以后学习和工作都有不错的参考意义,这部分有兴趣的同学还是 ...