Random模块作业 红包

import random

def red_func(money,countNum):

    havRedbag = []

    for i in range(1,countNum):
            select = random.uniform(0,money)
            ared = '{:.2f}'.format(select)
            ared=float(ared)
            # money += 0.01
            money -= ared
            havRedbag.append(ared)

    havRedbag.append(float('{:.2f}'.format(money)))
    random.shuffle(havRedbag)
    print(sum(havRedbag))

    return havRedbag


ret = red_func(100,10)
print(ret)

  

 

 

 

 

import random


def red_func(money, countNum):
    havRedbag = []
    s = money * 100
    for i in range(1, countNum):
        select = random.randint(1, s)
        s -=  select
        havRedbag.append(select)

    havRedbag.append(s)
    random.shuffle(havRedbag)
    print(sum(havRedbag))

    return havRedbag


ret = red_func(100, 10)
print(ret)

  

posted @ 2019-01-31 16:40  冥想心灵  阅读(129)  评论(0编辑  收藏  举报