I was trying to make a code for python where it asks for lines of the haiku, and than putting it into the file, and finally prints out to contents, and when I reuse the code, it overwrites the haiku already in the file, so I wanted the previous haiku’s to stay. Also this is in thonny.
Here is my code:
haiku = open('Haiku.txt', 'w')
line1 = input('What is line one of the haiku: ')
line2 = input('What is line two of the haiku: ')
line3 = input('What is line three of the haiku: ')
print('=====', file = haiku)
print(line1, file = haiku)
print(line2, file = haiku)
print(line3, file = haiku)
print('=====', file = haiku)
haiku.close()
Haiku = open('Haiku.txt').read()
print(Haiku)
and here is an output example:
>>> %Run 'Haiku Maker.py'
What is line one of the haiku: The evening night,
What is line two of the haiku: The moon shining in the dark,
What is line three of the haiku: A serene scene.
=====
The evening night,
The moon shining in the dark,
A serene scene.
=====
But when I add another haiku and re-run the code, it deletes previous ones, also I am a beginner, so try and be patient.
3