import random

class Card(object):

    def __init__(self, tuple):
        self.question = tuple[0]
        self.answer = tuple[1]

    def getQuestion(self):
        return self.question

    def getAnswer(self):
        return self.answer

#-----------------------------------------------------------------------

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))  # CHANGED
            flashCards.append(card)       # CHANGED
        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()       # ADDED
    correctAnswer = card.getAnswer()    # ADDED
    answer = raw_input(question + "\n") # CHANGED
    checkIfQuitting(answer)
    if answer == correctAnswer:         # CHANGED
        print "Right!"
        return
    print "No, that's wrong. Hint: First letter is:", correctAnswer[0] # CHANGED
    answer = raw_input(question + "\n") # CHANGED
    checkIfQuitting(answer)
    if answer == correctAnswer:        # CHANGED
        print "Right!"
        return
    while answer != correctAnswer:      # CHANGED
        print "Still wrong. Correct answer is:", correctAnswer  # CHANGED
        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!"

def displayFlashCards(flashCards):
    # Print out all the flash cards (mostly for debugging purposes).
    for card in flashCards:
        print card

main()

