0

I am creating a revision app and I would like to update the variable incorrect from 0 to 3 after user has answered all the questions. Data is stored as below in a json file. The number of times a question as been answered incorrectly is stored in memory as of now, but I would like it to be written into the json file so it is not lost after the program is closed. How can I do this?

TLDR: I want to update it from this:

"incorrect": [
        0,
        0,
        0,
        0
    ],

to this

"incorrect": [
        3,
        0,
        0,
        0
    ],
{
    "question": [
        "Q1. What is the equation for force?",
        "Q2. Define isotope",
        "Q3. Define wavelength",
        "Q4. Define modal dispersion"
    ],
    "answers": [

        "1",
        "1",
        "1",
        "1"

    ],
    "automark": [
        ["force=massxacceleration"],
        ["same number of protons", "different number of neutrons", "same element"],
        ["minimum distance between adjacent particles", "particles which are in phase"],
        ["different angles", "different times"]
    ],
    "markscheme": [
        ["force=massxacceleration"],
        ["same number of protons", "different number of neutrons", "same element"],
        ["minimum distance between adjacent particles", "particles which are in phase"],
        ["different angles", "different times"]
    ],
    "mastery": [
        0,
        0,
        0,
        0
    ],
    "incorrect": [
        0,
        0,
        0,
        0
    ],
    "correct": [
        0,
        0,
        0,
        0
    ],
    "partiallycorrect": [
        0,
        0,
        0,
        0
    ]
}

  • Load the JSON file into a variable, update the dictionary in the variable, then dump the variable back to the file. – Barmar Dec 29 '21 at 19:56
  • Read, modify, write. It's simple as that. BTW: I'd change that data structure. The question index should be the first level, the fields of each individual question should be the second. In your layout, it's vice-versa. The giveaway hint is that there are multiple arrays which all have the same size, because they each represent one aspect of multiple objects. – Ulrich Eckhardt Dec 29 '21 at 19:56
  • @UlrichEckhardt "if it ain't broke, dont fix it", I know what you mean and could probably do it, but I would need to change most of my code in order to make it compatible with the json file. Is there any benefit to doing it this way? – Rick Sanchez Dec 31 '21 at 12:36

0 Answers0