【发布时间】:2021-11-24 02:47:24
【问题描述】:
我编写了一个程序来掷骰子。它的工作方式是我将要滚动的骰子传递给主要功能,[4, 6] 又名 4 面骰子和 6 面骰子。
然后程序会生成 2 个列表,分别包含骰子的所有可能结果
[1, 2, 3, 4, 5, 6] and [1, 2, 3, 4]
然后,我列出所有可能的结果,并在两个列表之间添加每个数据组合。这是我的代码
#list of outcomes from current dice rolling - to contain sublists within it for each dice
rawDiceOutcomes = []
#list of total outcomes to find probabilities in
finalOutcomes = []
#generate n lists of outcomes
for i in range(len(dice)):
rawDiceOutcomes.append([i for i in range(1, dice[i]+1)]) #[1, 2, 3, 4], [1, 2, 3, 4, 5, 6] etc
#TODO : Figure out some way to do this for an n-dimensional list
for x in rawDiceOutcomes[0]:
for y in rawDiceOutcomes[1]:
tempOutcome = x + y
finalOutcomes.append(tempOutcome)
正如评论所示,如果只掷 2 个骰子,我知道该怎么做,但最终,我希望能够传递 3、4、100 个骰子等。我将如何遍历所有列表,所以我可以传递任意数量的骰子并且有效吗?
【问题讨论】: