In the context of an exercise on Bitcoin chains (hashing, etc.) I have a problem with the use of functions:
First, I define a function which modifies a list L0 to elaborate a list L1
But I have to create a 2nd function that iterates 10 times the function_1, replacing each time the initial list L0 by the list L1 produced in the previous round
for that I created the instruction L0=L1 at the end of the function_1
Then I don’t really know how to continue , I have tried :
def function_2_iteration (n): …for i in range (0,n): …function_1 (L0, values of variables …) function_2_iteration (10)
But I get an error message “L0 is not defined…”… in the call of function_1 in function_2
it seems that it is not possible to call a list by its name instead of its full expression
same problem if I put the loop in the function_1 without creating the function_2
You will find it easier to talk and reason about your code if you can
think of better names than “function_1”. Try to come up with names which
describe what the function does, if you can.
You have this function:
def function_2_iteration (n):
for i in range (0,n):
function_1 (L0, values of variables ..)
function_2_iteration (10)
But I get an error message “L0 is not defined…”… in the call of function_1 in function_2
Indeed. That’s because you don’t have a variable called L0 defined.
You need something like this:
L0 = [1, 2, 3, 4] # initial values for the list
for i in range(n):
L0 = function_1(L0, other variables...)
That starts with an initial value for the list L0, then each time
through the loop, the variable L0 gets replaced with the new values
calculated in function_1.
Hi steven
Thanks for your answer and your advice to better designate the functions …but to simplify I keep the names for the post
I am sorry but the proposed solution does not work:
at the 1st round L0 is reset to [1,2,3,4,5]
then there is a block because function_2 calls a calculation on a list in function_1
“File “D:\0-Python\test_8.py”, line 11, in tour_bloc l1[i]=l0[i]”
‘"TypeError:NoneType’ object is not subscriptable"
After searching the documentation, it appears that it is impossible to call a local variable, unless it is defined as global before
So I defined L1 as global , in function_1 (not possible for L0) and I could call it in function_2:
def function_2_iteration (n):
…for j in range (0,n):
…function_1(L1,[7, 11, 13, 17, 19, 23],100)
function_2_iteration (9)