Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A file visits.txt is made with "w", and the line Asha is written. Then the program is run again, and Ravi is written the same way. What does the file hold after the second run? What mode should the program use, if it must keep both names? Write the program for three visitors, so that running it twice gives six lines.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. The mode "w" empties the file at every open. After the second run, only Ravi is left.
To keep the old lines, use append, "a", which adds to the end.
Check. The first run makes the file (append creates it if needed) with 3 lines. The second run adds 3 more. After two runs the file holds 6 lines. (On a fresh disk, this program shows 3 after the first run and 6 after the second.)
Answer: With "w" the file holds only Ravi. Use "a" to keep both.
Log in to join the discussion.
Loading…