CIT 591 Assignment 2: Flash Cards
Fall 2011, David Matuszek

Purposes of this assignment:

General idea of the assignment:

You have probably used flash cards to help you memorize something. They have a "question" of some sort on one side, and an "answer" on the other. You use them by looking at the question, trying to remember the answer, then turning the card over to check your memory. If you were trying to learn the multiplication table, a card might have 6 x 9 on the front, and 54 on the back. Or, if you were trying to learn the U.S. state capitals, a card might have Pennsylvania on the front, and Harrisburg on the back.

Your assignment is to write a program to let you work with a set of "digital" flash cards.

Details:

In your first program, you didn't use functions, and your program consisted of statements. From here on, though, we will use a different structure: Your program will consist only of functions. One of your functions will be the "main" function (usually named main). You can load your program by hitting F5 in the editor window, then run it by typing main() at the shell prompt.

The program will shuffle the flash cards, then present them one at a time to the user. The user tries to type in the correct answer. If the user enters the correct answer, the program responds Right! and goes on to the next card.

With physical (cardboard) flash cards, after the user makes a guess, s/he turns the card over to see if the guess was correct. Because we are doing this with a computer, we can do better. The program will answer either Right! or Wrong and, in the case of a wrong guess, the program will give a hint. The hint will be the first character of the correct answer. So, for example, if the user is supposed to guess the capital of Pennsylvania, and guesses Philadelphia, the program can say something like No, that's wrong. Hint: The first letter is H. (See the example below.) If the user gets the answer wrong a second time, the program should tell the user the correct answer, and require him/her to type it in. (Typing it will help the user to remember it next time.)

You should also allow the user to quit at any type (by typing quit).

The program doesn't have to keep score--it doesn't tell the user what percent of answers s/he got right. (But you could add this feature if you want to.)

Here is an example of the use of the program. User input is in boldface .

>>> main()
Welcome to FlashCards!
Enter path to flash card file: state-capitals.txt
There are 50 flash cards.
You can enter the word 'quit' at any time.

Illinois
Springfield
Right!

Texas
Austin
Right!

Alaska
Nome
No, that's wrong. Hint: First letter is: J
Alaska
Juno
Still wrong. Correct answer is: Juneau
Now you type it: Juneau

Maine
Augusta
Right!
...and so on.

Your program should consist of a main() function and several additional functions, to be called from main (or perhaps from each other). Here's what each function should be called, what its parameter list should be, what it should do, and what it should return:

def main():
1. Print out a welcome message.
2. Ask for the name of a file containing flash cards.
3. Read in the flash cards.
4. Shuffle the flash cards (put them in random order).
5. Present each card in turn to the user, and handle the user's response.
6. Quit the program if the user enters 'quit' as a response.

Most of these steps are performed by calling another method or methods to do them.
def printWelcomeMessage():
Print out an appropriate message for starting the program. Returns None (in other words, doesn't return anything interesting).
def getFileName():
Ask the user for the name of a file, and return it (as a string).
def readFlashCards(fileName):
Returns a list of (question, answer) tuples. See below.
def shuffle(list):
Returns a new list containing all the elements of the given list, but in a randomized order. The original list should not be changed. (Hint: You can use the random.randint(min, max) method.)
def presentAllCards(listOfFlashCards):
Run through the list of flash cards, and present each card in the list to the user. Returns None.
def presentCard(flashCard):
Present the flash card to the user and handles his/her response. Returns None.
def checkIfQuitting(answer)
If the answer is 'quit', print some kind of "goodbye" message and return True, else return False.

Provided code

def readFlashCards(fileName):
    """Read in flash cards from the named file, and return as a list of tuples."""
    flashCards = []
    with open(fileName) as file:
        for line in file:
            line = line.strip()
            if len(line) > 0:
                flashCards.append(eval(line))
    return flashCards

Sample lists

state-capitals.txt
german-numbers.txt

Due date:

Before 6 AM, Friday September 23, via Sakai.