随机生成双色球号码和商品管理python程序

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/3
import random
def 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 result
def 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/3
import json
def 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 dic
def 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 False
def ispositiveinteger(N):
    N = str(N)
    if N.isdigit()and N != '0':
        return True
    else:
        return False
choice = 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

猜你喜欢

转载自www.cnblogs.com/arraon/p/10061765.html