zoukankan      html  css  js  c++  java
  • python 微信红包

    def redbags(money, num=10):
        import random
        choice = random.sample(range(1, money * 100), num - 1)
        choice.extend([0,money*100])
        choice.sort()
        return [(choice[i + 1] - choice[i]) / 100 for i in range(num)]
    while True:
        money = input('请输入你要发放的红包金额:').strip()
        num = input('请输入你要放的红包数量:')
        if money.isdigit() and num.isdigit():
            money = int(money)
            num=int(num)
            if money <= 0 or num <=0:
                print('输入金额或数量不能小于0')
                continue
            redbag = redbags(money,num)
            Max = max(redbag)
            nums = len(redbag)+1
            for index,p in enumerate(redbag,1):
                print('第%s个人获得红包%s元'%(index,p))
                if num == index:
                    break
        else:
            print('输入不合法')
            continue
    

      

    import random
    from time import sleep
    
    # 所有涉及金额的浮点数都需要用 round 方法保留2位小数,避免出现最终结果多出0.01
    amount = round(float(input('请设置红包的金额 > ')), 2)
    num = int(input('请设置红包的数量 > '))
    hb_dict = {}
    xing = '赵钱孙李周吴郑王'
    ming = '一二三四五六七八九十'
    
    while num:
    
        xingming = random.choice(xing) + random.choice(ming) + random.choice(ming)
        if xingming in hb_dict.keys():
            xingming = random.choice(xing) + random.choice(ming) + random.choice(ming)
    
        num -= 1
        if num == 0:
            print('%s抢到红包%.2f元 红包抢完了!' % (xingming, amount))
            hb_dict[amount] = xingming
            amount -= amount
        elif num > 0:
            hb = round(random.uniform(0.01, amount) / num, 2)
            hb_dict[hb] = xingming
            # 算法: 在0.01到红包总金额之间随机一个浮点数 / 红包剩余个数
            print('%s抢到红包%.2f元 剩余%d个!' % (xingming, hb, num))
            amount = round((amount - hb), 2)
    
        sleep(1)
    
    # 转置字典中的 key / value
    # hb_dict2 = {value:key for key,value in hb_dict.items()}
    max_hb = max(hb_dict.items())
    print('%s运气最佳 抢得%.2f元!!' % (max_hb[1], max_hb[0]))
    

      

  • 相关阅读:
    文件处理
    字符编码
    基本数据类型及内置方法
    python语法入门之流程控制
    python语法入门之基本数据类型
    python语法入门之用户交互、运算符
    编程语言与Python介绍
    计算机核心基础
    图片验证码推导逻辑,Image.new,ImageDraw, ImageFont.truetype的用法
    VUEday01
  • 原文地址:https://www.cnblogs.com/ipyanthony/p/9493789.html
Copyright © 2011-2022 走看看