The function iterates with print but not return

Python newbie working on 2.7.

I am trying to create a program that uses a function to generate text, and then outputs the text generated by the function to a file.

When you simply print a function in powershell (for example: http://codepad.org/KftHaO6x ), it iterates as I want:

def writecode (q, a, b, c):
    while b < q:
        b = b + 1
        print "v%d_%d_%d = pairwise (caps[%d],sals[%d],poss[%d],poss[%d],poss[%d],pos_range)" %(a,b,c,a,a,a,b,c)
        print "votes%d_%d.append(v%d_%d_%d)" % (b,c,a,b,c,)
        print "v%d_%d_%d = pairwise (caps[%d],sals[%d],poss[%d],poss[%d],poss[%d],pos_range)" %(a,c,b,a,a,a,c,b)
        print "votes%d_%d.append(v%d_%d_%d)" % (c,b,a,c,b)

writecode (5,1,0,4)

When trying to output a function to a file (for example: http://codepad.org/8GJpp9QY ), it gives only 1 value, that is, it does not iterate

def writecode (q, a, b, c):
    while b < q:
        b = b + 1
        data_to_write = "v%d_%d_%d = pairwise (caps[%d],sals[%d],poss[%d],poss[%d],poss[%d],pos_range)" %(a,b,c,a,a,a,b,c)
        data_to_write_two = "votes%d_%d.append(v%d_%d_%d)" % (b,c,a,b,c,)
        data_to_write_three = "v%d_%d_%d = pairwise (caps[%d],sals[%d],poss[%d],poss[%d],poss[%d],pos_range)" %(a,c,b,a,a,a,c,b)
        data_to_write_four = "votes%d_%d.append(v%d_%d_%d)" % (c,b,a,c,b)
        return data_to_write
        return data_to_write_two
        return data_to_write_three
        return data_to_write_four

x = writecode (5,1,0,4)

out_file = open("code.txt", "a")
out_file.write(x)
out_file.close()

Why is this, and how can I make the function of the output functions (for example, when printing)?

+5
source share
4 answers

, , ( return) while. , , - :

def writecode (q, a, b, c):
    results = []
    while b < q:
        b = b + 1
        results.append("v%d_%d_%d = pairwise (caps[%d],sals[%d],poss[%d],poss[%d],poss[%d],pos_range)" % (a,b,c,a,a,a,b,c))
        results.append("votes%d_%d.append(v%d_%d_%d)" % (b,c,a,b,c,))
        results.append("v%d_%d_%d = pairwise (caps[%d],sals[%d],poss[%d],poss[%d],poss[%d],pos_range)" % (a,c,b,a,a,a,c,b))
        results.append("votes%d_%d.append(v%d_%d_%d)" % (c,b,a,c,b))
        results.append("")
    return "\n".join(results)

x = writecode (5,1,0,4)

out_file = open("code.txt", "a")
out_file.write(x)
out_file.close()

, , , .

+2

, return :

def test():
    return 1
    return 2

x = test()
# x is now 1

yield, :

def this_will_work():
    yield 1
    yield 2

x = list(this_will_work()) # See below for why we are using list
# x is now [1, 2]

def so_will_this:
    return 1, 2

x = so_will_this()
# x is now (1, 2)

yield generator, - list, , . for ... in.

+5

return :

return ( None).

(, , list join ).

+1

; , .

, ( , b == c ).

from textwrap import dedent

codeblock = dedent("""\
    v{a}_{b}_{c} = pairwise (caps[{a}],sals[{a}],poss[{a}],poss[{b}],poss[{c}],pos_range)
    votes{b}_{c}.append(v{a}_{b}_{c})
    v{a}_{c}_{b} = pairwise (caps[{a}],sals[{a}],poss[{a}],poss[{c}],poss[{b}],pos_range)
    votes{c}_{b}.append(v{a}_{c}_{b})
""")

def get_code (q, a, b, c):
    return ''.join(codeblock.format(a=a, b=b, c=c) for b in xrange(b, q))

def main():
    with open('code.txt', 'a') as outf:
        outf.write(get_code(5,1,0,4))

if __name__=="__main__":
    main()
0

All Articles