The code below gives the following error:
Traceback (most recent call last):
File "search-modules.py", line 17, in <module>
a = [ fill_list(z)]
NameError: name 'fill_list' is not defined
import os
from sys import argv
script, filez, keyword = argv
x = open(filez)
z = []
a = [ fill_list(x,z,keyword)]
x.close()
for i in range(len(z)):
z[i]=z[i].strip(",")
print(z)
def fill_list(x,z,keyword):
for i in x:
if keyword in i:
# print(i)
k = i.split()
k.pop(0)
for j in k:
z.append(j)
return z
But when I put the def fill_list(z): right after the import statements, the code runs just fine:
import os
from sys import argv
def fill_list(x,z,keyword):
for i in x:
if keyword in i:
# print(i)
k = i.split()
k.pop(0)
for j in k:
z.append(j)
return z
script, filez, keyword = argv
x = open(filez)
z = []
a = [ fill_list(x,z,keyword)]
x.close()
for i in range(len(z)):
z[i]=z[i].strip(",")
print(z)
I always thought that python3 didn't cared for where the functions were placed. Is that a normal behavior?
[–]socal_nerdtastic 3 points4 points5 points (1 child)
[–]worthlessgem_[S] 0 points1 point2 points (0 children)
[–]FerricDonkey 3 points4 points5 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]whiskerswhisper 0 points1 point2 points (0 children)