CIT 591 Assignment 2: Flash Cards
Fall 2011, 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. 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():'quit' as a response. def printWelcomeMessage():None (in other words, doesn't return anything interesting). def getFileName():def readFlashCards(fileName):(question, answer) tuples. See below. def shuffle(list):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):None.def presentCard(flashCard):None.def checkIfQuitting(answer)'quit', print some kind of "goodbye" message and return True, else return False. 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
state-capitals.txt
german-numbers.txt