0

I have created a loop that will generate the code I need, however, it is stored as a string and I am unsure how to make this string into executable code.

I have tried eval() and exec() however it throws up an error.

Here's what I have:

for connum in range(farOut):
checker = (f"check{connum} = result['ask'].iloc[{connum}]")
print(checker)
connum=connum+1

I get the desired output printed (which varies in the number count):

> check0 = result['ask'].iloc[0] 
> check1 = result['ask'].iloc[1]
> check2 = result['ask'].iloc[2]

However, what I need to do is then run this check0, check1, check2... etc as code.

So, I need to get the correct result for checker from the output of check0, check1, check2 and so on.

I know I'm fairly close, just need the missing pieces. I tried eval() but it throws up an error:

eval(checker)

File "<string>", line 1
    check0 = result['ask'].iloc[0]
           ^
SyntaxError: invalid syntax

exec(checker) No error - just returns the output above:

> check0 = result['ask'].iloc[0] 
> check1 = result['ask'].iloc[1]
> check2 = result['ask'].iloc[2]

0 Answers0