在一对di上确定平均掷骰次数为11

2024-05-02 00:06:59 发布

您现在位置:Python中文网/ 问答频道 /正文

掷一对6面骰子(也叫D6),直到它们都是1。数一数这花了多少卷。 进行100次试验。打印出每卷的结果并报告所需的平均卷数。在

使用嵌套循环。外环进行100次试验;内环继续滚动直到出现1-1。然后更新运行计数,进行下一次试验。在

import random
dice1, dice2 = " ", " " 
roll = " "

for roll in range(1, 101):
    roll = 0
    dice1 = random.randint(1, 6)
    dice2 = random.randint(1, 6)
    print(dice1, ",", dice2)
    while dice1 == 1 and dice2 == 1:
         break

这不会停止时,2 1是一个掷骰,我需要帮助累积掷骰数和试用数


Tags: importfor报告random骰子计数嵌套循环roll
2条回答

问题是你的内环真的什么也做不了。 你必须给它你所描述的工作:继续掷两个骰子直到他们都是1。我将概述您描述的逻辑,但在实现时遇到困难。我把具体工作交给你。:-)

roll_count = 1
while not (dice1 == 1 and dice2 == 1):
    roll both dice
    increment roll_count

running_total += roll_count

您还需要在某处初始化running_total。在

这能帮你解开吗?在

import random
from itertools import count
for roll in range(1, 101):
    for c in count(1):
        dice1 = random.randint(1, 6)
        dice2 = random.randint(1, 6)
        print(c, ':', dice1, ",", dice2)
        if dice1 == 1 and dice2 == 1:
            break

相关问题 更多 >