Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A file marks.txt holds Asha:72, Ravi:85, Meera:64, one on each line. Write a function set_mark(name, mark) that changes the mark of a name in the file, or adds the name if it is not there, and keeps all the other lines. Test it with ("Ravi", 90) and ("Kabir", 50).
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. There is no way to change one line in the middle of a text file.
Check. Ravi changes from 85 to 90, and Kabir is added. The other two lines are kept: Asha 72 and Meera 64.
Answer: {'Asha': 72, 'Kabir': 50, 'Meera': 64, 'Ravi': 90}.
Log in to join the discussion.
Loading…