python字典操作+文件操作+函数
师从百测besttest
今天老牛教了些函数调用的知识,布置了个作业如下: # 1、写一个商品管理的小程序
# 2、商品存在文件里面
# 1、添加商品
# 输入产品名称、颜色、价格
# 要校验商品是否存在,价格是否合法
# 输入是否为空
# 2、修改商品信息
# 输入产品名称、颜色、价格
# 要校验商品是否存在,价格是否合法
# 输入是否为空
# 3、查看商品信息
# 输入商品名称
# 输入all就展示全部商品
# 输入的是其他的商品名称,
# 4、删除商品
# 输入商品名称 如果需要运行下列代码,请先在代码同级目录创建一个shangpin.txt文件,并写入一些内容,目前目录为空还没有做判断,如果为空会报错,内容例如:
{
"iphone": {
"color": "高亮黑",
"price": "99.99"
},
"电视": {
"color": "红色",
"price": "99.99"
},
"冰箱": {
"color": "白色",
"price": "99.99"
}
}
代码如下:
import json #读取商品信息
def goods():
with open('shangpin.txt', encoding='utf-8') as f:
str = json.load(f)
return str #判断商品是否存在
def is_good(name):
if name in goods():
return False
else:
return True #判断价格的正确性
def is_price(price):
price = price.strip()
if price and price.count('.') == 1:
left,right = price.split('.')
if left.isdigit() and right.isdigit() and int(right) < 100 and float(price) > 0:
return True
else:
return False
elif price.isdigit() and int(price) > 0:
return True
else:
return False #添加或修改商品信息
def updata_add(name,color,price):
all_goods = goods()
all_goods[name] = {'color': color, 'price': price}
with open('shangpin.txt', 'w', encoding='utf-8') as f:
json.dump(all_goods, f, ensure_ascii=False, indent=4) #添加商品
def inset_goods():
name = (input('请输入商品名称:')).strip()
color = (input('请输入颜色:')).strip()
price = (input('请输入价格:')).strip()
if name and color and price and is_good(name) and is_price(price):
updata_add(name,color,price)
print('添加成功!')
elif name and is_good(name) is False:
print('商品已存在!')
else:
print('输入错误!') #修改商品
def updata_goods():
name = (input('请输入商品名称:')).strip()
color = (input('请输入颜色:')).strip()
price = (input('请输入价格:')).strip()
if name and color and price and is_good(name) is False and is_price(price):
updata_add(name,color,price)
print('修改成功!')
elif name and is_good(name):
print('商品不存在!')
else:
print('输入错误') #查看商品信息
def see_goods():
goods_name = input('输入具体商品名称,要查看所有输入all:')
if goods_name.strip() != '':
if goods_name == 'all':
print(goods())
elif goods_name in goods():
print(goods_name,goods()[goods_name])
else:
print('没有这个商品!')
else:
print('输入错误!') #删除商品
def del_goods():
goods_name = (input('请输入商品名称:')).strip()
if goods_name.strip() != '':
if goods_name in goods():
all_goods = goods()
del all_goods[goods_name]
with open('shangpin.txt', 'w', encoding='utf-8') as f:
json.dump(all_goods, f, ensure_ascii=False, indent=4)
print('删除成功!')
else:
print('没有这个商品!')
else:
print('输入错误!') while True:
check = (input('请输入数字,1表示查询、2表示新增、3表示修改、4表示删除:')).strip()
if check.isdigit():
check = int(check)
if check == 1:
see_goods()
elif check == 2:
inset_goods()
print(goods())
elif check ==3:
updata_goods()
print(goods())
elif check == 4:
del_goods()
print(goods())
else:
print('输入错误请重新输入!')
else:
print('输入非数字!')
python字典操作+文件操作+函数的更多相关文章
- Python复习笔记-字典和文件操作
抽时间回顾2年前自己做过的python工具,突然感觉不像自己写的,看来好久没用过python的字典和文件操作了,查询资料和网页,整理如下: 一.字典 键值对的集合(map) 字典是以大括号“{}”包围 ...
- Python基础2 列表 元祖 字符串 字典 集合 文件操作 -DAY2
本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 ...
- 跟着ALEX 学python day3集合 文件操作 函数和函数式编程 内置函数
声明 : 文档内容学习于 http://www.cnblogs.com/xiaozhiqi/ 一. 集合 集合是一个无序的,不重复的数据组合,主要作用如下 1.去重 把一个列表变成集合 ,就自动去重 ...
- python基础之 列表、元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码
本节内容 列表.元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1. 列表.元组操作 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 ...
- python【第二篇】列表、元组、字典及文件操作
本节内容 列表 元组操作 字符串操作 字典操作 集合操作 文件操作 字符编码与转码 1.列表 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作:列表有序.可变.元素 ...
- python3笔记十八:python列表元组字典集合文件操作
一:学习内容 列表元组字典集合文件操作 二:列表元组字典集合文件操作 代码: import pickle #数据持久性模块 #封装的方法def OptionData(data,path): # ...
- Python的高级文件操作(shutil模块)
Python的高级文件操作(shutil模块) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 如果让我们用python的文件处理来进行文件拷贝,想必很多小伙伴的思路是:使用打开2个 ...
- python os&shutil 文件操作
python os&shutil 文件操作 # os 模块 os.sep 可以取代操作系统特定的路径分隔符.windows下为 '\\' os.name 字符串指示你正在使用的平台.比如对于W ...
- Python之路-文件操作(py3)
文件操作的基本步骤: 1.打开文件:f=open('filename'),with open('filename') as f 2.操作文件:增,删,改,查 3.关闭文件:f.close 打开文件 p ...
- python 历险记(三)— python 的常用文件操作
目录 前言 文件 什么是文件? 如何在 python 中打开文件? python 文件对象有哪些属性? 如何读文件? read() readline() 如何写文件? 如何操作文件和目录? 强大的 o ...
随机推荐
- java中list和map的底层实现原理
Collection(单列集合) List(有序,可重复) ArrayList 底层数据结构是数组,查询快,增删慢 线程不安全,效率高 Vector 底层数据结构是数组,查询快,增删慢 线程安全,效率 ...
- 【细谈Java并发】谈谈LinkedBlockingQueue(转)
最近在看concurrent包的知识,看到LinkedBlockingQueue,发现一篇好文推荐给大家.原文地址:[细谈Java并发]谈谈LinkedBlockingQueue 1.简介 上篇我们介 ...
- binlog2sql闪回工具的使用
binlog2sql闪回工具的使用 一.下载安装依赖的python yum install openssl-devel bzip2-devel expat-devel gdbm-devel readl ...
- IDEA配置和插件
1.相关配置 设置字体和大小 2.插件 maven helper 解决maven包冲突的问题 打开pom文件,并可以切换tab,简单使用,如下图 RestfulToolkit RestfulToolk ...
- 'telnet' is not recognized as an internal or external command
http://blog.csdn.net/lubinsu/article/details/7294870 使用telnet的时候出现如下提示:'telnet' is not recognized ...
- webpack给目录起别名
1. 配置文件目录: build>webpack.base.config.js: resolve: { alias: { '@': resolve('src'), 'styles': resol ...
- vim.rc配置(deepin)
set nocompatible " be iMproved, requiredfiletype off " required " set the runtime pat ...
- openstack导入镜像
本文以制作CentOS7.2镜像为例,详细介绍手动制作OpenStack镜像详细步骤,解释每一步这么做的原因.镜像上传到OpenStack glance,支持以下几个功能: 支持密码注入功能(nova ...
- Codeforces 601B. Lipshitz Sequence(单调栈)
Codeforces 601B. Lipshitz Sequence 题意:,q个询问,每次询问给出l,r,求a数组[l,r]中所有子区间的L值的和. 思路:首先要观察到,斜率最大值只会出现在相邻两点 ...
- 利用ceph-deploy部署ceph存储集群
一.环境准备 创建两台主机,ip地址和主机名如下 192.168.2.100,主机名ceph-1 192.168.2.101,主机名ceph-2 每个主机 新增加一块数据盘,分区根据自己需要分区即可, ...