gpt4 book ai didi

python - 打印骰子掷出所有 6 个面的概率

转载 作者:行者123 更新时间:2023-12-01 00:31:01 24 4
gpt4 key购买 nike

我正在尝试打印公平骰子滚动时的预期数量,并继续滚动直到所有 6 个面都滚动为止。

我想做的是,作为一个例子,当滚动 1 时,它会被添加到列表中。然后继续滚动,直到所有 6 个面都滚动完毕,然后使用 count+=1 继续滚动下一个骰子,并使用 count 作为次数。然后,一旦列表等于[1,2,3,4,5,6],使 stop 等于 True 并中断。

但是我应该使用移位方法,以便如果找到 3 则将其从列表中删除吗?

最后完成后,我想使用 count 计算预期的卷数

import random
def rolldice():
count = 0
while True:
die = []
win = []
for i in range(1):
die.append(random.choice([1,2,3,4,5,6]))
stop = False
for roll in die:
number = die.count(roll)
if(number == 1):
win += [1]
if(number == 2):
win += [2]
if(number == 3):
win += [3]
if(number == 4):
win += [4]
if(number == 5):
win += [5]
if(number == 6):
win += [6]
if(win == [1,2,3,4,5,6]):
stop = True
break
if stop:
break
else:
count += 1
print(f'Count is {count}')
def main():
rolldice()
main()

尝试看看我是否走在正确的轨道上,或者是否应该使用移动和删除。

最佳答案

如果您只想计算获得所有六个面需要多少次,那么使用一组会更容易:

import random

# initialize to empty set
results = set()

# remember how many rolls we have made
rolls = 0

# keep going until we roll all six sides
while len(results) != 6:
rolls += 1
results.add(random.choice([1,2,3,4,5,6]))

print('It took %d rolls to get all six sides' % rolls)

关于python - 打印骰子掷出所有 6 个面的概率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58177263/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com