python微信红包_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]))

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值