CIT 591 Assignment 2: Flash Cards
Fall 2009, 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.


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. Program output is brown, user input is green.

>>> main()
Welcome to FlashCards!
Enter path to flash card file: state-capitals.txt
There are 50 flash cards.
How many times would you like to go through them? 1
You can also enter the word 'quit' at any time.



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

...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). Just what functions you define is up to you (but see my style rules page). To help get you started, I'll tell you what functions I wrote; you can try to write these same functions, or you can create a different set.

Here's what my version of this method does:
  1. Prints out a welcome message.
  2. Asks for the name of a file containing flash cards.
  3. Reads in the flash cards (I will provide a function for doing this).
  4. Asks how many times the user wants to go through the flash cards.
  5. Goes through a loop the requested number of times. Each time:
    1. Shuffles the flash cards (put them in random order)
    2. Presents each card in turn to the user, and handle the user's responses
Most of these steps are performed by calling another method or methods to do them.
Randomizes the order of items in the list. (Uses the randint function mentioned in problem 29 on page 25.) Returns None.
Reads in the flash cards from the given file, and returns a list of tuples (see below). Since we haven't yet talked about how to read from files, here's the code (Changes are in red.):
def readFlashCards(fileName):
    flashCards = []
    file = open(fileName)
    flashCard = file.readline()
    while flashCard:
        flashCard = flashCard.strip() # remove line endings
	        if len(flashCard) > 0:     # ignore blank lines
            flashCards.append(eval(flashCard)) # notice the 'eval'
        flashCard = file.readline()
    return flashCards
Runs through the list of flash cards, and presents each card in the list to the user. Returns None.
Presents the flash card to the user and handles his/her responses. Returns None.
If the answer is 'quit', just prints a message and calls quit(). When using IDLE, this doesn't quit the program as cleanly as I would like, but it does work.

My program assumes that the file contains tuples, one tuple per line, so what the method returns is a list of tuples. A tuple is just an immutable list. Tuples are written with parentheses instead of brackets (for example, ("pi", 3.1416) instead of ["pi", 3.1416]). This is probably all you need to know about tuples for this assignment, but see pages 74-75 in the textbook if you want to know more.

Sample lists


Due date:

Before 6 AM, Friday September 25, 2009, via Blackboard.