随机生成双色球号码和商品管理python程序,双色球python,1、写一个程序,输入
随机生成双色球号码和商品管理python程序,双色球python,1、写一个程序,输入
1、写一个程序,输入N就产生N条双色球号码
红球 6 01-33
蓝球 1 01-16
产生的双色球号码不能重复,写到一个文件里面,每一行是一条
红球: 01 03 05 07 08 18 蓝球:16
红球需要排序,是升序
2、写一个商品管理的程序:
1、添加商品
商品名称:商品已经存在的话,要提示
商品价格: 校验,是大于0的数字
商品数量:校验,只能是大于0的整数
2、删除商品
商品名称:商品不存在的话,要提示
3、查看商品
显示所有的商品信息
4、退出
def add_product():
pass
def del_product():
pass
def show_product():
pass
choice = input(‘请输入你的选择:‘)
代码1:
#_author:‘ZYB‘#data:2018/12/3import randomdef RedBall(N): R = set() while len(R) != N: start = random.randint(1, 33) R.add(‘%02d‘ % start) L = sorted(list(R)) result = ‘ ‘.join(L) return resultdef DoubleColorBall(count): results = set() while len(results) != count: starts = RedBall(6) end = random.randint(1, 16) res = ‘红球:%s 蓝球:%02d\n‘ % (starts, end) results.add(res) with open(‘shuangseqiu.txt‘, ‘w‘, encoding=‘utf-8‘) as fw: fw.writelines(results)DoubleColorBall(500)
代码2
#_author:‘ZYB‘#data:2018/12/3import jsondef readjson(): try: with open(‘product.json‘, ‘a+‘, encoding=‘utf-8‘) as f: res = json.load(f) # load可以读文件 return res except json.decoder.JSONDecodeError: #不加此句,json读取空文件会报错! dic={} writejson(dic) return dicdef writejson(dic): fw = open(‘product.json‘, ‘w‘, encoding=‘utf-8‘) json.dump(dic, fw, indent=4, ensure_ascii=False) fw.close()def add_product(name,prices,counts): res = readjson() d1 = {} d2 = d1.setdefault(name, {}) d2.setdefault(‘price‘, prices) d2.setdefault(‘count‘, counts) if res.get(name): print(‘商品已存在‘) else: res[name] = d1[name] writejson(res)def del_product(name): res = readjson() if res.get(name): res.pop(name) writejson(res) else: print(‘商品不存在‘)def show_product(): res = readjson() print(res)def ispostiveNum(N): if N.replace(".", ‘‘).isdigit() and N != ‘0‘: if N.count(".") == 0 or N.count(".") == 1: return True else: return Falsedef ispositiveinteger(N): N = str(N) if N.isdigit()and N != ‘0‘: return True else: return Falsechoice = input(‘请输入你的选择[1:添加商品;2:删除商品;3:显示商品;4:退出]:‘)if choice == ‘1‘: name = input(‘请输入商品名称:‘) price = input(‘请输入商品价格:‘) if ispostiveNum(price): count = input(‘请输入商品数量:‘) if ispositiveinteger(count): add_product(name,price,count) else: print(‘输入商品数量格式不正确!‘) else: print(‘输入商品价格格式不正确!‘)elif choice == ‘2‘: name = input(‘请输入要删除商品的名称:‘) del_product(name)elif choice == ‘3‘: show_product()else: pass
随机生成双色球号码和商品管理python程序
相关内容
- python 读取中文CSV 'gbk' codec can't de
- python random 的用法,pythonrandom用法,python ran
- 玩了下opencv的aruco(python版),opencvaruco,简单的玩了下
- python编码iso-8859-9编码问题,pythoniso-8859-9,(2018-10-1
- “本办法学python” ex49,本办法学pythonex49,1.创建目录结
- Python系列之——利用Python实现微博监控,python,0x00 前言
- python 多线程队列套接字,python多线程队列,#server端im
- centos 7 编译 python3.7.0,centospython3.7.0,安装编译所需库
- python匹配两个字符串中间的字符串,python匹配,问题:使
- Pycharm+Python+PyQt5使用,pycharmpyqt5,转载自http://
评论关闭