Home > Net >  I can't run a function in a loop for certain amount of time
I can't run a function in a loop for certain amount of time

Time:01-01

ok, so I have this function that consists in a game that will always return a random value,

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:

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

CodePudding user response:

In short:

The dictionary premios doesn't have the 12 key.

There is a possibility that movimientos[-1] is equal to 12 inside your function. Thus premios[movimientos[-1]] will raise a KeyError: 12.

Go through the code:

Each time, you are appending either 1 or 2 to movimientos.

Then, casilla chooses a value from movimientos[i] or movimientos[i] 1. After that, casilla is appended to movimientos.

Here is the problem:

For a sufficiently large iteration, If casilla keeps choosing movimientos[i] 1, there is a chance that elements in movimientos goes beyond 11. Once that happened, since 12 doesn't exist in premios, key error happens.

Potential solution:

You may want to use random.randint() for choosing a random iteger instead.

for i in range(10):

    casilla = random.randint(1, 11)

    movimientos.append(casilla)

Also, juego()['premio'] is unnecessarily complicated. The loop can also be implemented inside the function as well. In this way, you can input a number, n, to the function, and the function will run n times.

CodePudding user response:

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.

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.

And since premios does not contain a defenition with 12 as a key, it will give you an error.

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 :)

  • Related