Python新手在这里,运行2.7。
我正在尝试创建一个使用函数生成文本的程序,然后将函数生成的文本输出到文件中。
当仅在 powershell 中打印函数时(例如:http ://codepad.org/KftHaO6x ),它会按照我的意愿进行迭代:
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)
当试图将函数输出到文件中时(如:http ://codepad.org/8GJpp9QY ),它只给出 1 个值,即不迭代:
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()
为什么会这样,我怎样才能使输出函数迭代(就像打印一样)?