[英]Trying to generate n random numbers that result in a fixed sum
我一直在嘗試獲取一個總計為一定數量的隨機整數列表(不使用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)
主要()
但我不斷得到范圍不是可調用的錯誤。 我究竟做錯了什么?
您正在調用num_dish
,就好像它是一個函數一樣,但實際上它是一個變量,它現在存儲使用range(1, dishes)
創建的range對象。 我不確定您要通過該程序實現什么目標,但是此注釋應該會對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.