[英]I can't run a function in a loop for certain amount of time
ok, so I have this function that consists in a game that will always return a random value,好的,所以我有这个 function,它包含在一个总是返回随机值的游戏中,
def juego():
movimientos = []
premio = 0
premios = {1:10, 2:5, 3:2, 4:1, 5:0.5, 6:0, 7:0.5, 8:1, 9:2, 10:5, 11:10}
primer_movimiento = random.choice([1,2])
secuencia = []
if primer_movimiento == 1:
secuencia.append('Izquierda')
else:
secuencia.append('Derecha')
movimientos.append(primer_movimiento)
for i in range(10):
casilla = random.choice([movimientos[i],movimientos[i]+1])
movimientos.append(casilla)
for j in range(1,11):
if movimientos[j] == movimientos[j-1]:
secuencia.append('Izquierda')
else:
secuencia.append('Derecha')
premio = premios[movimientos[-1]]
movimiento_premio = {'premio': premio, 'secuencia':secuencia, 'movimientos':movimientos }
return movimiento_premio
and the thing is that I want to run this function 10,000 times to receive 10,000 different values, and it works when I run it individually, but when I run it with a loop, it shows me this error:问题是我想运行这个 function 10,000 次以接收 10,000 个不同的值,当我单独运行它时它可以工作,但是当我用循环运行它时,它会显示这个错误:
Loop:环形:
premio_acumulado = []
for i in range(10000):
premio_acumulado.append(juego()['premio'])
And here's the error it presents:这是它出现的错误:
KeyError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_22100/4261353443.py in <module>
3 for i in range(10000):
4
----> 5 premio_acumulado.append(juego()['premio'])
6
7
~\AppData\Local\Temp/ipykernel_22100/2828093291.py in juego()
39
40
---> 41 premio = premios[movimientos[-1]]
42
43 movimiento_premio = {'premio': premio, 'secuencia':secuencia, 'movimientos':movimientos }
KeyError: 12
The dictionary premios
doesn't have the 12
key.词典
premios
没有12
键。
There is a possibility that movimientos[-1]
is equal to 12
inside your function. Thus premios[movimientos[-1]]
will raise a KeyError: 12
. movimientos[-1]
有可能在您的 function 中等于12
因此premios[movimientos[-1]]
将引发 KeyError KeyError: 12
。
Each time, you are appending either 1
or 2
to movimientos
.每次,您都将
1
或2
附加到movimientos
。
Then, casilla
chooses a value from movimientos[i]
or movimientos[i] + 1
.然后,
casilla
从movimientos[i]
或movimientos[i] + 1
中选择一个值。 After that, casilla
is appended to movimientos
.之后,
casilla
被附加到movimientos
。
For a sufficiently large iteration, If casilla
keeps choosing movimientos[i] + 1
, there is a chance that elements in movimientos
goes beyond 11
.对于足够大的迭代,如果
casilla
一直选择movimientos[i] + 1
,则movimientos
中的元素有可能超过11
。 Once that happened, since 12
doesn't exist in premios
, key error happens.一旦发生这种情况,由于
premios
中不存在12
,因此会发生密钥错误。
You may want to use random.randint()
for choosing a random iteger instead.您可能希望使用
random.randint()
来选择随机整数。
for i in range(10):
casilla = random.randint(1, 11)
movimientos.append(casilla)
Also, juego()['premio']
is unnecessarily complicated.此外,
juego()['premio']
不必要地复杂。 The loop can also be implemented inside the function as well.该循环也可以在 function 内部实现。 In this way, you can input a number,
n
, to the function, and the function will run n
times.这样,您可以向function输入一个数字
n
,function将运行n
次。
Ok so as I see it, remember I'm not very fluent with this language, and I might not be completely accurate, but:好吧,正如我所见,请记住我对这种语言不是很流利,而且我可能不完全准确,但是:
What I believe is wrong is that, because premios
only has from 1 - 11 and does not contain "12" as a key, it is giving you an error.我认为错误的是,因为
premios
只有 1 - 11 并且不包含“12”作为键,所以它给你一个错误。
So in this loop:所以在这个循环中:
for i in range(10):
casilla = random.choice([movimientos[i],movimientos[i]+1])
movimientos.append(casilla)
Since primer_movimiento
is returning random.choice([1, 2])
, movimientos[i]+1
is not returning from 1, 11 instead it is returning 1, 12.由于
primer_movimiento
正在返回random.choice([1, 2])
, movimientos[i]+1
不会从 1, 11 返回,而是返回 1, 12。
And since premios does not contain a defenition with 12 as a key, it will give you an error.而且由于 premios 不包含以 12 作为键的定义,它会给你一个错误。
Instead replace:而是替换:
for i in range(10):
casilla = random.choice([movimientos[i],movimientos[i]+1])
movimientos.append(casilla)
with和
for i in range(9):
casilla = random.choice([movimientos[i],movimientos[i]+1])
movimientos.append(casilla)
It is NOT a time issue:)这不是时间问题:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.