I'm trying to save the output of the for loop in a text file along with its corresponding variables. However, when I do the following I only get the last line as an output.
a = [1,2,3,4]
b = [4,5,6,7]
c = [5]
file=open("my_output.txt", 'a')
for i, j in zip(a, b):
z = (i**2)*j+c[0]
print (z)
z = str(z)
file.write(z + "\n")
file.close()
My output:
117
What I'm looking for:
a,b,c,z
1,4,5,9
2,5,5,25
3,6,5,59
4,7,5,117
Would appreciate any support. Thank you in advance.
c = [5]? you can usec= 5printcan write to files? Using f-string you can just print to file. See my example below.