简体   繁体   English

Python 上的掷骰子统计数据

[英]Dice rolls stats on Python

I am having some trubles trying to figure out how this function works:我有一些麻烦试图弄清楚这个 function 是如何工作的:

import control

 def sevenStats(numRolls):
     sevenCount=0
     for i in range (numRolls):
         roll = control.rollDie(6) + control.rollDie(6)
         if roll == 7
             sevenCount += 1
      return sevenCount

In particular I didn't catch what this line does:特别是我没有明白这条线的作用:

 roll = control.rollDie(6) + control.rollDie(6)

The purpose of the overall function should be to keep track of a given number's rolls, in this case the number = 7.整体 function 的目的应该是跟踪给定数字的滚动,在这种情况下,数字 = 7。

Can you help me go trough this?你能帮我解决这个问题吗? Thank you.谢谢你。

I guess that the function control.rollDie(num) generates a random number between 1 and the indicate number, in this case 6. It roll two dices thats why it add it 2 times.我猜想 function control.rollDie(num) 会生成一个介于 1 和指示数之间的随机数,在本例中为 6。它掷了两个骰子,这就是它加 2 次的原因。 Ans then it counts how many times the sum (roll) is equal to 7. Ans 然后它计算总和(滚动)等于 7 的次数。

The function is counting the times you obtain a 7 when throwing two dices in n tries. function 正在计算您在 n 次尝试中掷两个骰子时获得 7 的次数。

roll = control.rollDie(6) + control.rollDie(6)

That line of code sums the values of the two dices you have throw.那行代码将您掷出的两个骰子的值相加。

control.rollDie(6) generates a random number between 1 and the value given in the parameter which in this case is 6. control.rollDie(6)生成一个介于 1 和参数中给出的值之间的随机数,在本例中为 6。

roll = control.rollDie(6) + control.rollDie(6)

So what this line does is that it generates two random numbers between 1 and 6 and then adds them together and stores the value in the variable roll所以这条线的作用是它生成两个介于 1 和 6 之间的随机数,然后将它们相加并将值存储在变量roll

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM