随机红包

出自:http://www.nonb.cn/blog/python-bonus.html
import random,sys
def HB(min,max,total,num):
print(min,max,total,num)
total = float(total)
num = int(num)
min = 0.01

if num < 1: #人数小于1就直接退出
return
if num == 1: #个数为1则金额为全部金额
print("第%s个人拿到的红包数:%.2f"%(num,total))
return
i = 1
totalMoney = total
while i < num: #红包个数至少大于1
max = totalMoney - min * (num-i) #最大包=红包总额-最小包(红包个数-1)
k = int((num-i)/2)
if num - i <=2:
k = num - 1
max = max/k
money = random.randint(int(min*100),int(max*100))
money = float(money)/100
totalMoney = totalMoney - money
print('第%d个人拿到的红包是:%.2f;余额为:%.2f'%(i,money,totalMoney))
i +=1
print('第%d个人拿到的红包是:%.2f;余额为:%.2f'%(i,totalMoney,0.00))

if __name__ == '__main__':
# min = sys.argv[1]
# max = sys.argv[2]
# total = sys.argv[3]
# num = sys.argv[4]
HB(0.01,10,20,4)
# HB(min,max,total,num)
posted @ 2017-12-04 15:38  13684995613  阅读(176)  评论(0编辑  收藏  举报