#、添加
#、商品名称
#、要从文件里面把所有的商品读出来
#、价格
#、写一个方法判断是否为合理的价格
#、数量
#整数
# 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写商品管理练习的更多相关文章

  1. python作业/练习/实战:3、实现商品管理的一个程序

    作业要求 实现一个商品管理的一个程序,运行程序有三个选项,输入1添加商品:输入2删除商品:输入3 查看商品信息1.添加商品: 商品名称:xx 商品如果已经存在,提示商品已存在 商品价格:xx数量只能为 ...

  2. oldboy s21day12.设计商城系统,主要提供两个功能:商品管理、会员管理。

    #!/usr/bin/env python# -*- coding:utf-8 -*- # 1.写出三元运算的基本格式及作用?'''a if a>b else b''' # 2.什么是匿名函数? ...

  3. 快速掌握用python写并行程序

    目录 一.大数据时代的现状 二.面对挑战的方法 2.1 并行计算 2.2 改用GPU处理计算密集型程序 3.3 分布式计算 三.用python写并行程序 3.1 进程与线程 3.2 全局解释器锁GIL ...

  4. 文献综述一:基于UML技术的商品管理系统设计与实现

    一.基本信息 标题:基于UML技术的商品管理系统设计与实现 时间:2018 出版源:福建电脑 文件分类:uml技术的研究 二.研究背景 使用 UML 技术对商品管理系统进行了分析与研究,使用户对商品信 ...

  5. 用python写windows服务

    用python写windows服务(1) 以python2.5 为例需要软件 * python 2.5        * pywin32(与2.5 版本相匹配的) Service Control Ma ...

  6. Python的包管理工具

    Python的包管理工具 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.为什么使用包管理 Python的模块或者源文件直接可以复制到目标项目目录中,就可以导入使用了. 但是为了 ...

  7. 谈一谈Python的上下文管理器

    经常在Python代码中看到with语句,仔细分析下,会发现这个with语句功能好强,可以自动关闭资源.这个在Python中叫上下文管理器Context Manager.那我们要怎么用它,什么时候用它 ...

  8. Python PIP包管理器

    版权所有,未经许可,禁止转载 章节 Python 介绍 Python 开发环境搭建 Python 语法 Python 变量 Python 数值类型 Python 类型转换 Python 字符串(Str ...

  9. 用python写网路爬虫 PDF高清完整版免费下载 Python基础教程免费电子书 python入门书籍免费下载

    <用python写网路爬虫PDF免费下载>PDF书籍下载 内容简介 作为一种便捷地收集网上信息并从中抽取出可用信息的方式,网络爬虫技术变得越来越有用.使用Python这样的简单编程语言,你 ...

随机推荐

  1. zabbix3.4 监控网卡流量设置差量

    自定义监控项的时候 看里面预设的监控网卡下载流量的key是 net.if.in[if,<mode>]这次可以监控流量了 但是添加监控项的时候 看网上别人的 都是可以设置数值是每秒的差量 出 ...

  2. WebClient请求接口,get和post方法

    1,get方式 string URI = "url"; //实例化 WebClient client = new WebClient(); // client.UseDefault ...

  3. 关于json_encode转数组为json对象时里有数组格式数据的问题

    前言:这次是给一款小程序提供接口时发现的 别的不多说,下面直接看出现问题的json数据 可以看到,这是一个大的json对象,是由多维数组组成,一般api接口提供的也是这种格式. 但是仔细看红框中的内容 ...

  4. linux系统调用之进程控制

    1 进程控制: fork                                                                                     创建一 ...

  5. 关于继承的基本知识,方法重写,final和abstract的使用, 动态绑定和静态绑定的知识

    一.继承: 涉及关键字: extends(继承) super final abstract 特点: 1.类与类之间可以用 XX是XX来描述 , 那么他们之间就存在继承关系.  2.Java中不支持多继 ...

  6. java linux sdk1.8

    wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-co ...

  7. (简单贪心) 发工资咯:) hdu2021

    发工资咯:) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  8. day-02(css,js)

    本文档并非个人所写,只是便于参考:回顾: html: 作用:展示 文件标签: <html> <head> <title></title> </he ...

  9. Go-day01

    Go-实现hello_world package main import "fmt" func main() { fmt.Println("hello world!&qu ...

  10. Linux安装npm并打包前端代码

    查看node版本$ node -v查看npm版本$ npm -v如果没有安装node及npm,需要先安装node及npm#yum install node# yum install npm安装cnpm ...