Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How do you add to something in a save file?

+1 vote
asked Mar 15, 2020 by A coder in need
I am making a game where the user has to guess a word and every time they win,  a win counter in a save file will increase by one. Please explain how, when the user joins and plays for the first time, the win counter in the save file will be 0 and once they win, the win counter goes up to 1.

2 Answers

0 votes
answered Jun 13, 2021 by ITSME (760 points)
0 votes
answered Jun 13, 2021 by Peter Minarik (88,520 points)

I'm not working with Python on a daily basis. You can find any reference or tutorial pages that will tell you how to use files.

However, I'll tell you a simple logic (with pseudo-code) of how to handle your problem

function WhenGameBegins
    if CanOpen([in] saveFileName, [out] saveFile)
        winCount = ReadWinCountFrom(saveFile)
        winCount = 0

function WhenGameEnds
    WriteWinCountInto([in] saveFile, [in] winCount)

So the point is that when the user plays for the very first time, no save file exists, so you know that your winCount is 0. Any other time, you can read it from this save file.

When the game ends, you save the winCount into a save file (even if it is 0, though if it's the only thing you store there, there isn't much difference between saving and not saving -- see why above). The next time the user plays, the value can be accessed from the file.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.