试图生成n个随机数,导致固定的总和

问题描述 投票:0回答:1

我一直试图得到一个随机整数列表(不使用numpy多项式),总结到一定数量。我知道之前已经问过这个问题,但我一直在讨论问题,因为我希望用户能够定义要输出的数字范围/数量,而不是预设数字。

import random

def dishes():
    plates = int(input("How many dishes can you make: "))
    return plates

def budget():
    total = int(input("How much money do you have to spend: "))
    return total    


def restnum(dishes, total):
    num_dish = range(1, dishes)
    dividers = sorted(random.sample(num_dish(1, total), dishes - 1))
    return [a - b for a, b in zip(dividers + [total], [0] + dividers)]

def main():
    dishes_val = dishes()
    total_val = budget()
    restnum(dishes_val, total_val)

主要()

但我不断得到范围是不可调用的错误。我究竟做错了什么?

python
1个回答
1
投票

您正在调用num_dish,就好像它是一个函数,但它实际上是一个变量,现在存储使用range(1, dishes)创建的范围对象。我不确定你试图用这个程序实现什么,但这个评论应该对你有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.