Python 试图生成n个随机数,结果是一个固定的和
我一直在尝试获取一个随机整数列表(不使用numpy多项式),这些整数的总和为某个数字。我知道以前有人问过这个问题,但我一直在讨论这些问题,因为我希望用户能够定义要输出的数字的范围/数量,而不是预设的数字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
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)
创建的范围对象。
我不确定你想通过这个项目实现什么,但是这个评论应该对你有所帮助