The biggest error is in td[s-nd]+=1
, this even causes you to try to access indexes out of range. In case this does not happen, the value of an item in the list will not save the output of random
. It should be something like td[i] = s
.
In addition to this, you mix several possible approaches to the problem in one, we will see several options to see if something is clear:
-
List with n
launches in which each element is the sum of the dice, a call to random
for each launch:
-
Using indices:
nd = int(input("Cuantos dados quieres tirar? "))
nv = int(input("Cuantas veces quieres tirar los dados? "))
td = [0] * nv
for i in range (nv):
td[i] = random.randint(nd, (6 * nd))
print(td)
-
Using list.append
:
nd = int(input("Cuantos dados quieres tirar? "))
nv = int(input("Cuantas veces quieres tirar los dados? "))
td = []
for _ in range (nv):
td.append(random.randint(nd, (6 * nd)))
print(td)
-
Compression Lists:
nd = int(input("Cuantos dados quieres tirar? "))
nv = int(input("Cuantas veces quieres tirar los dados? "))
td = [random.randint(nd, (6 * nd)) for _ in range(nv)]
print(td)
-
List with n
launches in which each element is the sum of the dice, a call to random
for each die.
-
Using indices:
nd = int(input("Cuantos dados quieres tirar? "))
nv = int(input("Cuantas veces quieres tirar los dados? "))
td = [0] * nv
for i in range (nv):
for _ in range(nd):
td[i] += random.randint(1, 6)
print(td)
-
Using list.append
:
nd = int(input("Cuantos dados quieres tirar? "))
nv = int(input("Cuantas veces quieres tirar los dados? "))
td = []
for _ in range (nv):
s = 0
for _ in range(nd):
s += random.randint(1, 6)
td.append(s)
print(td)
-
Using list compression:
nd = int(input("Cuantos dados quieres tirar? "))
nv = int(input("Cuantas veces quieres tirar los dados? "))
td = [sum(random.randint(1, 6) for _ in range(nd)) for _ in range(nv)]
print(td)
-
A list with a nested list for each roll containing the value of each of the dice.
-
Using indices:
nd = int(input("Cuantos dados quieres tirar? "))
nv = int(input("Cuantas veces quieres tirar los dados? "))
td = [[0] * nd for _ in range (nv)]
for i in range (nv):
for j in range(nd):
td[i][j] = random.randint(1, 6)
print(td)
-
Using list.append
nd = int(input("Cuantos dados quieres tirar? "))
nv = int(input("Cuantas veces quieres tirar los dados? "))
td = []
for _ in range (nv):
s = []
for _ in range(nd):
s.append(random.randint(1, 6))
td.append(s)
print(td)
-
Using list compression
nd = int(input("Cuantos dados quieres tirar? "))
nv = int(input("Cuantas veces quieres tirar los dados? "))
td = [[random.randint(1, 6) for _ in range(nd)] for _ in range(nv)]
print(td)
It should be said that compression lists are undoubtedly the most efficient options.
For the first two approximations we obtain for 2 dice and 5 throws for example:
[8, 4, 11, 5, 7]
For the last one:
[[2, 1], [2, 3], [5, 5], [6, 4], [3, 5]]