I am having some trubles trying to figure out how this function works:
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.
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. Ans then it counts how many times the sum (roll) is equal to 7.
The function is counting the times you obtain a 7 when throwing two dices in n tries.
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.