如何在列表中的数字之间分配值

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

我正在创建一个偏向骰子滚动模拟器,我希望用户:

1.输入他们想要改变概率的数字

2.输入概率(小数形式)

然后我希望我的程序在其他值之间分配余数,这是我的第一篇文章 - 如果需要任何其他信息请告诉我

我的代码:

choice = int(input('Which number would you like to change to the probability of?:  '))

prob = int(input('Probability: '))

probs = [0,0,0,0,0,0]

probs[choice] = prob
python list math statistics probability
2个回答
1
投票

这是一种方法:

choice = 2
prob = 20

choices = []
for i in range(6):
    if i == choice:
        choices.append(prob)
    else:
        choices.append((100 - prob) / 5)
print(choices)

输出:

[16.0, 16.0, 20, 16.0, 16.0, 16.0]

为了计算概率,我们将余数 (

100 - prob
) 除以面数以与 (
5
) 共享余数。

我们循环骰子的每一面。如果是要修改的脸,我们设置为

prob
,否则我们设置为余数的五分之一


-2
投票
filler_list = []
while sum(filler_list) < variable_to_distribute:
    for filler_index in range(list_length):
        if len(filler_list) < list_length:
            if sum(filler_list) < variable_to_distribute:
                filler_list.append(1)
            else:
                filler_list.append(0)
        elif sum(filler_list) < variable_to_distribute:
            filler_list[filler_index] += 1
if sum(filler_list) != variable_to_distribute:
    etext = f"""La cantindad variable_to_distribute:{variable_to_distribute} no se ha distribuido correctamente"""
    raise ErrorC(etext)
© www.soinside.com 2019 - 2024. All rights reserved.