Flashcard Game: How to keep score
Question: I'm trying to build a flashcard game in python for python terms. A few of the
specs I'd like it to have are:

1) The program should store scores in a file 2) Scores should keep track of
each word and / or key and keep track of how many times guessed correct and
incorrect 3) After each guess, the program should tell the user how many times
the user has guessed that key correctly and incorrectly

I'm very new to Python and would appreciate the simplest explanation on how to
make this work (if at all possible).

Here's what I have:

    
    
    from random import choice
    import sys
    
    print("Welcome! Please type 'Start' to begin or 'Quit' to leave")
    
    user_answer = raw_input()
    
    if (user_answer == "quit"):
        quit()  
    
    file = []
    
    words = {
        "break": "Stops repeat of a block or loop", 
        "else": "comes after 'if' to offer an alternative option", 
        "if": "informs computer on how to react depending on key response from user",
        "index": "Return the index in the list of the first item whose value is x",
        "dict": "Associates one thing to another no matter what it is",
        "import": "To call a command",
        "def": "to define a word",
        "print": "to send a message to the screen for the user to see",
        "for": "One way to start a loop",
        "while": "Another way to start a loop",
        "elif": "When an 'If'/'Else' situation calls for more than one 'Else'",
        "from": "directs the computer to a location from which to import a command"
    }
    
    score = file[]
    
    key = choice(words.keys())
    remaining_questions = 3
    
    while remaining_questions > 0:
        print("Which command can accomplish: " + words[key] + "...?")
        user_guess = raw_input()
        print(str(user_guess == key))
        remaining_questions = remaining_questions - 1
    


Answer: Well, let's start with the basic: you can store the result in a dict as well
and use the `pickle` module to dump that dict into a file. So, the dict
structure can be like this:

    
    
    answers[word]['correct']
    answers[word]['wrong']
    

I'll make a simple and short code to start that dict with its default values
(0s for all the words and guesses) if the file doesn't exist and to load that
dict from a file if it does exist, using `Pickle`:

    
    
    from os.path import isfile
    from pickle import load
    
    if isfile('answers.bin'):
        # loads the dict from the previously "pickled" file if it exists
        answers = load(open('answers.bin', 'rb'))     
    else: 
        # create a "default" dict with 0s for every word
        for word in words.keys():
            answers[word] = {'correct': 0, 'wrong': 0}
    

Then, we will build a `if` statement to check is the user answered correctly:

    
    
    if key == user_guess: # there's no need to cast the `user_guess` to str
        answers[key]['correct'] += 1
    else:
        answers[key]['wrong'] += 1
    

Finally, after the while block, we persist that answers dict with pickle, so
we can load it later if needed:

    
    
    from pickle import dump
    dump(answers, open('answers.bin', 'wb'))
    

