Im a beginner at Python and I have written a program consisting of three functions and now Im stuck when I need to 'call' on them at the bottom of the program. In the 1st and 2nd functions I get some information and saving it to a file, but the important thing is what I get from fcn3. Fcn2 needs the result from fcn1 and fcn3 needs the result from fcn2. So they all rely on each other(thats why im writing them to a new file, to save and to reuse). It looks something like this:

def fcn1():
    do some stuff
    write result to a new file

def fcn2():
    do some stuff
     write result to a new file

def fcn3():
    do some stuff
    write result to a new file
    return result

#___________main___________
f=open('filename','r'
fread=f.readlines()
for f in fread
    fcn1()
    fcn2()
    res=fcn3()

res

All the functions seem to work, but not when I run the entire program called prob.py. I have several files I want to go through and that I need f to open. How do I automate things? Each filename has same directory, but differences in an ID nr.

Recommended Answers

All 2 Replies

You have to return the values, see http://www.penzilla.net/tutorials/python/functions/ for starters.

def fcn1(record):
    do some stuff
    write result to a new file
    return data_from_this_function
     
def fcn2(data_from_fcn1):
    do some stuff
    write result to a new file
    return data_from_this_function
     
def fcn3(data_from_fcn2):
    do some stuff
    write result to a new file
    return result
     
#___________main___________
f=open('filename','r'
fread=f.readlines()
for f in fread
    res1=fcn1(f)
    res2=fcn2(res1)
    res=fcn3(res2) 

##------- you can run the following
def func_1(list_in):
    list_in.append("added by func_1")
    return list_in

def func_2(list_in):
    for ctr in range(len(list_in)):
        if list_in[ctr] == "added by func_1":
            list_in[ctr]=list_in[ctr].replace("func_1", "func_2")
    return list_in

def func_3(list_in):
    list_in.append("ADDED by func_3")
    return list_in


res_1=func_1([])
print res_1
res_2=func_2(res_1)
print res_2
res=func_3(res_2) 
print res

Also you will get better answers if you post your code instead of pseudo code. This issue was already mentioned a few days ago, and it's not a difficult one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.