out_file = open('result.txt', 'w')
A = [1,2,3,4,5,6,7,8,9,10]
B = [11,12,13,14,15]
for a in A:
for b in B:
result = a + b
print (result, file = out_file)
out_file.close()
The above program writes one out file (result.txt) consisting of all the results (50 elements) together.
I want to write ten out files each consisting of 5 elements and named as follows:
1.txt
2.txt
...
10.txt
The 1.txt file will put the sum of 1+11, 1+12, 1+13, 1+14, and 1+15.
The 2.txt file will put the sum of 2+11, 2+12, 2+13, 2+14, and 2+15.
.....
The 10.txt file will put the sum of 10+11, 10+12, 10+13, 10+14, and 10+15.
Any help, please. Very simple program is expected.
Again, when I wanted to name the out file using elements of N, why I could not?
A = [1,2,3,4,5,6,7,8,9,10]
B = [11,12,13,14,15]
N = ['a','b','c','d','e','f','g','h','i','j']
for a in A:
results = []
for b in B:
result = a + b
results.append(result)
for n in N:
with open('{}.txt'.format(n),'w') as f:
for res in results:
f.write(str(res)+'\n')