码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-12-04 00:04:21      阅读:411      评论:0      收藏:0      [点我收藏+]

标签:dig   with   排序   提示   post   put   als   error   default   

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

 

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

标签:dig   with   排序   提示   post   put   als   error   default   

原文地址:https://www.cnblogs.com/arraon/p/10061765.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!