Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 试图生成n个随机数,结果是一个固定的和_Python - Fatal编程技术网

Python 试图生成n个随机数,结果是一个固定的和

Python 试图生成n个随机数,结果是一个固定的和,python,Python,我一直在尝试获取一个随机整数列表(不使用numpy多项式),这些整数的总和为某个数字。我知道以前有人问过这个问题,但我一直在讨论这些问题,因为我希望用户能够定义要输出的数字的范围/数量,而不是预设的数字 import random def dishes(): plates = int(input("How many dishes can you make: ")) return plates def budget(): total = int(input("How m

我一直在尝试获取一个随机整数列表(不使用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)
main()


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

您正在调用
num\u dish
,好像它是一个函数,但实际上它是一个变量,现在存储了使用
范围(1,disks)
创建的范围对象。 我不确定你想通过这个项目实现什么,但是这个评论应该对你有所帮助