python写商品管理练习
#、添加
#、商品名称
#、要从文件里面把所有的商品读出来
#、价格
#、写一个方法判断是否为合理的价格
#、数量
#整数
# product = {
# "爱疯差":{
# "price":999.98,
# "count":
# },
# "car":{
# "price":,
# "count":
# }
# }
# product['mac'] = {"price":,"count":} # write(product)
# 写入文件,最新的商品写进去
#、删除
# 、商品名称
# 、要从文件里面把所有的商品读出来
# product = {
# "爱疯差": {
# "price": 999.98,
# "count":
# },
#
# }
# product.pop('car') #、查询
# 、要从文件里面把所有的商品读出来 FILENAME = 'product.json'
import json
import os
def get_product():
with open(FILENAME,'a+',encoding='utf-8') as fr:
fr.seek()
content = fr.read()
if content:
res = json.loads(content)
else:
res = {}
return res def is_price(s):
s=str(s)
if s.count('.')==:
left,right = s.split('.')
if left.isdigit() and right.isdigit():
print('正小数')
return float(s)
elif s.isdigit():
if int(s)>:
print('大于0的整数')
return int(s)
return False def is_count(s):
if s.isdigit():
if int(s)>:
return int(s) def write_product(product_dic):
with open(FILENAME,'w',encoding='utf-8') as fw:
json.dump(product_dic,fw,ensure_ascii=False,indent=) def add():
all_products = get_product()
pname = input('product_name:').strip()
price = input('product_price:').strip()
count = input('product_count:').strip()
if not pname or not price or not count:#为空的时候干啥
print('不能为空!')
elif pname in all_products:
print('商品已经存在')
elif not is_price(price):
print('价格不合法,只能是大于0的数值')
elif not is_count(count):
print('数量不合法!')
else:
all_products[pname] = {"price": float(price), "count": int(count)}
write_product(all_products)
print('添加商品成功')
return
return add() # if pname and price and count: #不为空的时候,我干啥。。 def delete():
all_products = get_product()
pname = input('product_name:').strip()
if not pname :#为空的时候干啥
print('不能为空!')
elif pname not in all_products:
print('商品不存在')
else:
all_products.pop(pname)
write_product(all_products)
print('删除商品成功')
return
return delete() def show():
all_products = get_product()
if all_products:
print(all_products)
else:
print('暂时还没有商品!') choice = input('1、add\n'
'2、delete\n'
'3、show \n'
'4、exit \n') func_map = {"":add,"":delete,"":show,"":quit}
if choice in func_map:
func_map[choice]()
else:
print('输入有误!') # if choice =="":
# add()
# elif choice=="":
# delete()
# elif choice=="":
# show()
# elif choice=="":
# quit("程序退出")
# else:
# print('输入错误!') # def a():
# print('asdfdfs')
#
# b = a
# b()
#函数即变量
python写商品管理练习的更多相关文章
- python作业/练习/实战:3、实现商品管理的一个程序
作业要求 实现一个商品管理的一个程序,运行程序有三个选项,输入1添加商品:输入2删除商品:输入3 查看商品信息1.添加商品: 商品名称:xx 商品如果已经存在,提示商品已存在 商品价格:xx数量只能为 ...
- oldboy s21day12.设计商城系统,主要提供两个功能:商品管理、会员管理。
#!/usr/bin/env python# -*- coding:utf-8 -*- # 1.写出三元运算的基本格式及作用?'''a if a>b else b''' # 2.什么是匿名函数? ...
- 快速掌握用python写并行程序
目录 一.大数据时代的现状 二.面对挑战的方法 2.1 并行计算 2.2 改用GPU处理计算密集型程序 3.3 分布式计算 三.用python写并行程序 3.1 进程与线程 3.2 全局解释器锁GIL ...
- 文献综述一:基于UML技术的商品管理系统设计与实现
一.基本信息 标题:基于UML技术的商品管理系统设计与实现 时间:2018 出版源:福建电脑 文件分类:uml技术的研究 二.研究背景 使用 UML 技术对商品管理系统进行了分析与研究,使用户对商品信 ...
- 用python写windows服务
用python写windows服务(1) 以python2.5 为例需要软件 * python 2.5 * pywin32(与2.5 版本相匹配的) Service Control Ma ...
- Python的包管理工具
Python的包管理工具 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.为什么使用包管理 Python的模块或者源文件直接可以复制到目标项目目录中,就可以导入使用了. 但是为了 ...
- 谈一谈Python的上下文管理器
经常在Python代码中看到with语句,仔细分析下,会发现这个with语句功能好强,可以自动关闭资源.这个在Python中叫上下文管理器Context Manager.那我们要怎么用它,什么时候用它 ...
- Python PIP包管理器
版权所有,未经许可,禁止转载 章节 Python 介绍 Python 开发环境搭建 Python 语法 Python 变量 Python 数值类型 Python 类型转换 Python 字符串(Str ...
- 用python写网路爬虫 PDF高清完整版免费下载 Python基础教程免费电子书 python入门书籍免费下载
<用python写网路爬虫PDF免费下载>PDF书籍下载 内容简介 作为一种便捷地收集网上信息并从中抽取出可用信息的方式,网络爬虫技术变得越来越有用.使用Python这样的简单编程语言,你 ...
随机推荐
- bzoj1398 Necklace
关于最小表示法的模板题. 最小表示法:把一个字符串表示为它的的所有循环同构字符串中的字典序最小者. 直接参见代码中的函数getmin()获取精髓 #include <cstdio> #in ...
- (转)轻松学,Java 中的代理模式及动态代理
背景:讲到反射机制,肯定会想到动态代理. 轻松学,Java 中的代理模式及动态代理 代理模式可以在不修改被代理对象的基础上,通过扩展代理类,进行一些功能的附加与增强.值得注意的是,代理类和被代理类应该 ...
- windows 匿名管道: 父进程与子进程通信 (进程间通信之CreatePipe)
看了很多篇相关的资料,感觉这个还是比较靠谱的: 进程间通信之CreatePipe https://blog.csdn.net/dacxu/article/details/30071081 特别是 ...
- 真机控件获取 app-inspector
1.安装app-inspector:npm install app-inspector -g 若是要卸载原有的:npm uninstall app-inspector -g np ...
- 关于Nginx负载均衡的5种策略
nginx的upstream目前支持的5种方式的分配 1.轮询(默认) 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. upstream backserver { ...
- JVM 辣鸡回收
垃圾回收算法 标记清除法 先标记出需要回收的对象,然后一次性回收.缺点:会产生内存碎片,并且效率也不高. 标记压缩法 先标记出需要回收的对象,然后让存活对象向一端移动,移动的过程中进行回收辣鸡.避免了 ...
- 无法删除foo.length
- java静态变量和final关键字
静态变量其实就是全局变量 静态方法不需要实例化对象,直接可以调用. public class StaticVariable { public static int num1=20; public vo ...
- 点赞功能与redis
转:https://edu.aliyun.com/a/20538 摘要: 前言点赞其实是一个很有意思的功能.基本的设计思路有大致两种, 一种自然是用mysql等数据库直接落地存储, 另外一种就是利用点 ...
- 对manacher的一点感性理解
因为总是忘掉板子所以这里贴一下我个人对\(manacher\)的感性理解. 可能不够严谨求轻喷\(QwQ\) char ch = getchar (); s[0] = s[1] = '#'; whil ...