I have a string of words which are displayed in a pyramid form in the python shell, but I have a problem when I try to move them to a .txt file.
The problem is that the program takes each of the characters and writes them in the new file, but it writes only on the first line and just replaces the previous line with the new one and in the end the file contains only the last line.
I've tried adding +'\n'
to the write function but the result is that the pyramid loses its alignment.
If it is possible to select the first, the second and so on line in a text file and write to them that would be the solution, but I cannot find a solution like that.
The part of the code looks like this:
def pyramid(text):
for i in text:
line = string.center(i)
afile = open("name.txt", "w")
for row in text:
afile.write(row)
the input "text" is a nested list with the words.