Flash card challenge: Difference between revisions
imported>Jesstess |
imported>Jesstess No edit summary |
||
Line 92: | Line 92: | ||
===Checking your work=== |
===Checking your work=== |
||
Try out your script on the following quiz files: |
|||
<pre> |
|||
$ python scrabble.py |
|||
Usage: scrabble.py [RACK] |
|||
</pre> |
|||
<pre> |
|||
$ python scrabble.py AAAaaaa |
|||
2 aa |
|||
</pre> |
|||
<pre> |
|||
$ python scrabble.py ZZAAEEI |
|||
22 zeze |
|||
21 ziz |
|||
12 zee |
|||
12 zea |
|||
11 za |
|||
3 aia |
|||
2 ee |
|||
2 ea |
|||
2 ai |
|||
2 ae |
|||
2 aa |
|||
</pre> |
|||
===Bonus challenge=== |
===Bonus challenge=== |
||
Modify your script to quiz based on either the question or answer. e.g. for state capitals, the quizzer would present either a state, expecting its capital as the answer, or a capital, expecting its state as the answer. |
|||
Modify your script to handle blank tiles. Blank tiles have a score of 0 but can be used to represent any letter. |
|||
===Congratulations!=== |
===Congratulations!=== |
Revision as of 20:25, 8 July 2012
Project
Write a flash card quizzer from scratch.
Goals
- practice breaking down a problem and solving it in Python from scratch
- practice command line option parsing
- practice reading from files
- practice working with dictionaries and for loops
Problem statement
Write a Python script that takes a file as an argument and quizzes the user based on the contents of that file until the user quits the program. Questions should be selected randomly (as opposed to going in order through the file), and the user should type in their guess. The script should say whether or not a guess is correct and provide the correct answer if an incorrect answer is given.
The file will contain flash card challenges in the form:
question,answer question,answer question,answer question,answer ...
For example, a state capitals flash card file might have the form:
Alabama,Montgomery Alaska,Juneau Arizona,Phoenix ...
Running the quizzer script with this file might look like this:
$ python quizzer.py state_capitals.txt Texas? Austin Correct! Nice job. New Mexico? Santa Fe Correct! Nice job. Oregon? Portland Incorrect. The correct answer is Salem. Virginia? Richmond Correct! Nice job. Virginia? Exit Goodbye
Breaking down the problem
= Step 1: Get the questions from a fixed flash card file
Write the code to open and read a specific hardcoded flash card file (we'll deal with getting the filename from the user later). Create a dictionary, where each comma-separated question and answer become a key and value in the dictionary. Note that each line in the file ends in a newline, which you'll need to strip.
Step 1 resources:
- File input and output: http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files.
- Stripping characters (like whitespace and newlines) from a string: http://docs.python.org/library/stdtypes.html#str.strip.
Step 2: Randomly select questions from the question dictionary
Write a while
loop that loops forever and at each iteration through the loop randomly selects a key/value pair from the questions dictionary and prints the question.
Step 3: Get and check the user's answer
Inside your while
loop, write the code that gets an answer from the user and compares it to the answer retrieved from the questions dictionary. If the answer is correct, say so. If the answer is incorrect, say so and print the correct answer.
Step 4: Allow the user to quit the program
The while
loop currently runs forever. Pick a special phrase (like "Exit") that the user can type instead of an answer that signals that they want to quit the program. When that special phrase is given, print a goodbye message and break
out of the while
loop to end the program.
Step 5: Get the quiz questions file from the user
Write the code to get the quiz questions file from a command line argument. Handle the case where a user forgets to supply a file.
Step 5 resources:
- Command line argument parsing: http://docs.python.org/library/argparse.html#module-argparse.
- Getting and checking the number of command line arguments: http://docs.python.org/library/sys.html.
Checking your work
Try out your script on the following quiz files:
Bonus challenge
Modify your script to quiz based on either the question or answer. e.g. for state capitals, the quizzer would present either a state, expecting its capital as the answer, or a capital, expecting its state as the answer.
Congratulations!
You've implemented a substantial, useful script in Python from scratch. Keep practicing!