import random

class Card(object):

    def __init__(self, tuple):
        self.question = tuple[0]
        self.answer = tuple[1]
        self.timesRight = 0
        self.timesWrong = 0

    def getQuestion(self):
        return self.question

    def getAnswer(self):
        return self.answer

    def markRight(self):
        self.timesRight += 1

    def markWrong(self):
        self.timesWrong += 1

    def __str__(self):
        return '[%5s --> %5s] %2d right, %2d wrong' % \
            (self.question, self.answer, self.timesRight, self.timesWrong)

    # Declare a class variable 
    presentations = 0

    # Define two class methods
    def bumpPresentationCount(cls):
        cls.presentations += 1

    bumpPresentationCount = classmethod(bumpPresentationCount)

    def getPresentationCount(cls):
        return cls.presentations

    getPresentationCount = classmethod(getPresentationCount)

#-----------------------------------------------------------------------

def shuffle(list):
    # Shuffle the elements of the list.
    # NOTE: I didn't know about random.shuffle() when I wrote this.
    for n in range(1, len(list)):
        i = random.randint(0, len(list) - 1)
        j = random.randint(0, len(list) - 1)
        list[i], list[j] = list[j], list[i]

def readFlashCards(fileName):
    # Reads and returns a list of 2-tuples from the given file.
    flashCards = []
    file = open(fileName)
    flashCard = file.readline()
    while flashCard:
        flashCard = flashCard.strip()
        if len(flashCard) > 0:
            card = Card(eval(flashCard))
            flashCards.append(card)
        flashCard = file.readline()
    file.close()
    return flashCards
    
def checkIfQuitting(answer):
    # Raise an exception if the user wants to quit.
    if answer == 'quit':
        print "Quitting."
        raise StandardError

def presentCard(card):
    # Presents the card to the user repeatedly until s/he
    # enters a correct answer.
    print
    question = card.getQuestion()
    correctAnswer = card.getAnswer()
    answer = raw_input(question + "\n")
    Card.bumpPresentationCount()        # ADDED
    checkIfQuitting(answer)
    if answer == correctAnswer:
        print "Right!"
        card.markRight()
        return
    print "No, that's wrong. Hint: First letter is:", \
          correctAnswer[0]
    card.markWrong()
    answer = raw_input(question + "\n")
    checkIfQuitting(answer)
    if answer == correctAnswer:
        print "Right!"
        return
    while answer != correctAnswer:
        print "Still wrong. Correct answer is:", correctAnswer
        answer = raw_input("Now you type it: ")
        checkIfQuitting(answer)
    
def presentAllCards(flashCards):
    # Presents each card in turn.
    for card in flashCards:
        presentCard(card)

def main():
    print "Welcome to FlashCards!"
    fileName = raw_input("Enter path to flash card file: ")
    flashCards = readFlashCards(fileName)
    print "There are", len(flashCards), "flash cards."
    timesThrough = input("How many times would you like to go through them? ")
    print "You can also enter the word 'quit' at any time."
    for i in range(0, timesThrough):
        shuffle(flashCards)
        try:
            presentAllCards(flashCards)
        except StandardError, e:
            print "Error!", e
            break
    print "\nAll done!"
    print displayFlashCards(flashCards)
    print '\n%d presentations in all.' % Card.getPresentationCount() # ADDED

def displayFlashCards(flashCards):
    # Print out all the flash cards (mostly for debugging purposes).
    for card in flashCards:
        print card

main()


