CIT 591 Assignment 2: Flash Cards
Fall 2009, David Matuszek
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.
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). 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.
main()shuffle(list)list. (Uses the randint function mentioned in problem 29 on page 25.) Returns None.readFlashCards(fileName)
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()
file.close()
return flashCards
presentAllCards(listOfFlashCards)None.presentCard(flashCard)None.checkIfQuitting(answer)'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.
state-capitals.txt
german-numbers.txt
2009-holidays.txt