【问题标题】:Rolling 4 dices 1000 times and counting the number of times the sum of the four dices' score is equal to 21 or higher掷 4 个骰子 1000 次,计算四个骰子的总和等于或大于 21 的次数
【发布时间】:2021-02-23 13:17:38
【问题描述】:

所以,我正在学习 Python 中的随机化,并想通过制作一种“骰子模拟器”程序来测试我的知识,该程序将四个“骰子”投掷 1000 次;然后计算四个骰子的总和等于或大于 21 的次数。这是我到目前为止所得到的:

import random

dice1 = random.randint(1,6)
dice2 = random.randint(1,6)
dice3 = random.randint(1,6)
dice4 = random.randint(1,6)

sum = dice1 + dice2 + dice3 + dice4

n = 0    # the amount of times the sum of the score was 21 or higher

for i in range(1000):
    dice1 = random.randint(1,6)
    dice2 = random.randint(1,6)
    dice3 = random.randint(1,6)
    dice4 = random.randint(1,6)

    for sum >= 21:
        n = n + 1

print("You got 21 or higher", n, "times.")

但是,程序的结果窗口只显示“你得到 21 或更高的 0 次。”“你得到 21 或更高的 1000 次。”。所以,我想我的问题是:如何修复代码,以便计算 1000 次“掷骰子”中得分总和为 21 或更高的次数,在结果窗口中打印所述次数,我的代码中的哪一行需要修复(又名:哪里出错了)?提前致谢!

【问题讨论】:

  • 你确定你写的是for sum >= 21:吗?这不是有效的 Python 语法。 for 应该是 if。另一个问题是您忘记在for 循环内更新变量sum,因此它保持其初始值(第一次滚动的四个骰子的总和)。
  • 您没有在 for 循环中更新 sum,顺便说一句,避免使用此名称,因为它会影响内置 sum

标签: python for-loop while-loop probability dice


【解决方案1】:

您应该在for 循环内更新变量sum。否则,它保持其初始值,即第一轮中四个骰子的总和。

请注意,它们是一个名为sum 的python 内置函数,并且为变量使用内置名称是非常糟糕的做法。下面,我将变量重命名为sumOfDice

import random

n = 0    # the amount of times the sum of the score was 21 or higher

for i in range(1000):
    dice1 = random.randint(1,6)
    dice2 = random.randint(1,6)
    dice3 = random.randint(1,6)
    dice4 = random.randint(1,6)
    
    sumOfDice = dice1 + dice2 + dice3 + dice4
    
    if sumOfDice >= 21:
        n = n + 1

print("You got 21 or higher", n, "times.")

其他改进

当您开始使用名称中带有数字的变量时,您应该问自己:骰子真的需要四个命名变量吗?使用一个列表来保存四个值不是更容易吗?如果dice 是一个列表,那么您可以像dice[0]dice[1] 等一样访问各个值。但是您也可以使用循环列表推导和其他很酷的python 功能来操作列表。你甚至可以调用 python 内置函数sum 来获取列表的总和!!

import random
n = 0
for i in range(1000):
    dice = [random.randint(1,6) for _ in range(4)]
    if sum(dice) >= 21:
        n = n + 1
print("You got 21 or higher, {} times.".format(n))

【讨论】:

  • +1 表示 N 掷骰子列表。在这种特殊情况下,我们甚至可以写成rolls_sum = 4 * random.randint(1, 6),因为平均结果是一样的。
  • @Guimute 错了。我们对平均结果不感兴趣,我们在计算结果超过 21 的次数。想象一下,如果我掷硬币,一边是“0”,一边是“10”,然后问你“多少次值是否大于 7?” 0 和 10 的平均值是 5,5 永远不会超过 7。但正确答案应该是翻转次数的一半左右。
  • 啊,我明白了!感谢您的输入和关于高效 Python 功能的提示 Stef!我觉得现在实现上述功能对我来说可能有点过头了,因为我刚刚开始使用 Python 进行编码,但是当我使用起来更舒服时,我一定会记住它以备将来使用语言! :-)
  • @quasireal 即使您是初学者,我仍然建议您使用dice = [random.randint(1,6) for _ in range(4)]; sumOfDice = dice[0] + dice[1] + dice[2] + dice[3],而不是非常重复的dice1 = random.randint(1,6); dice2 = random.randint(1,6); dice3 = random.randint(1,6); dice4 = random.randint(1,6); sumOfDice = dice1 + dice2 + dice3 + dice4
【解决方案2】:

我解决了这个问题。你可以试试这个

import random

n = 0   

for i in range(1000):
    dice1 = random.randint(1,6)
    dice2 = random.randint(1,6)
    dice3 = random.randint(1,6)
    dice4 = random.randint(1,6)
    sum = dice1 + dice2 + dice3 + dice4
    if sum >= 21:
        n = n + 1

print("You got 21 or higher", n, "times.")

【讨论】:

  • 啊啊,我看到 for 循环中的 sum 定义是我所缺少的......现在我已经添加了它,它运行完美,谢谢 Cenk! :D
【解决方案3】:

您需要在 for 循环中计算骰子的总和。

在您的示例中,总和始终等于对 random.randint 的前 4 次调用中生成的随机数的总和,但您应该每次都重新计算总和。

此外,在您拥有for sum >= 21 的行中,for 应替换为iffor 用于重复,if 用于条件执行。

【讨论】:

    【解决方案4】:

    您不需要定义骰子,每个random.randint(1,6) 本身就是一个骰子。

    import random
    
    n = 0    # the amount of times the sum of the score was 21 or higher
    
    for i in range(1000):
        sum = random.randint(1,6) + random.randint(1,6) + random.randint(1,6) + random.randint(1,6)
    
        if sum >= 21:
            n = n+1
    
    print("You got 21 or higher", str(n), "times.")
    

    这是一种 Python 方式:

    dice_sum = [int((x/x)) for x in range(1,1001) if  random.randint(1,6) + random.randint(1,6) + random.randint(1,6) + random.randint(1,6) >= 21]
    
    print("You got 21 or higher", str(sum(dice_sum)), "times.")
    

    我所做的只是在每次骰子为>= 21 时获取索引,然后将其除以相同的索引,使其返回 1 到列表中,然后我得到列表的总和,即多少次1 已添加到列表中,这是骰子出现>= 21 的次数。

    【讨论】:

      猜你喜欢
      • 2020-06-06
      • 2018-12-17
      • 1970-01-01
      • 2016-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-21
      相关资源
      最近更新 更多