Flash card challenge: Difference between revisions

m
Reverted edits by 31.25.3.98 (talk) to last revision by Jesstess
imported>Jesstess
imported>Jeremyb
m (Reverted edits by 31.25.3.98 (talk) to last revision by Jesstess)
 
(8 intermediate revisions by 2 users not shown)
Line 1:
[[File:Flashcards.jpg|right|300px]]
 
== Project ==
 
Line 50 ⟶ 52:
Download http://web.mit.edu/jesstess/www/IntermediatePythonWorkshop/state_capitals.txt.
 
Write the code to open and read <code>state_capitals.txt</code> (we'll deal with getting a variable 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 stripremove from the word.
 
<b>Step 1 resources</b>:
Line 66 ⟶ 68:
 
Write a <code>while</code> 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.
 
To randomly select a key from the dictionary, you can use the <code>random</code> module, and in particular the <code>random.choice</code> function.
 
When you run your script, to break out of the <tt>while</tt> loop you can press <tt>Control</tt> and then (while still holding down Control) <tt>c</tt>.
 
<b>Step 2 resources</b>:
<ul>
<li>
<code>while</code> loops: http://en.wikibooks.org/wiki/Python_Programming/Flow_control#While_loops
</li>
<li>
Dictionary manipulation: http://docs.python.org/tutorial/datastructures.html#dictionaries. In particular, look at getting a list of the dictionary's keys using the <code>keys</code> method.
</li>
Selecting a random value from a list using the <code>random</code> module: http://docs.python.org/library/random.html#random.choice
</li>
</ul>
 
 
Line 71 ⟶ 89:
 
Inside your <code>while</code> 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.
 
You can get input from a user using the <code>raw_input</code> function.
 
It is up to you how strict you want to be with a user's answer. Do you want capitalization to matter?
 
<b>Step 3 resources</b>:
<ul>
<li>
Using <code>raw_input</code> to get data from the user: http://docs.python.org/library/functions.html#raw_input
</li>
</ul>
 
 
Line 76 ⟶ 105:
 
The <code>while</code> 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 <code>break</code> out of the <code>while</code> loop to end the program.
 
<b>Step 4 resources</b>:
<ul>
<li>
Using the <code>break</code> keyword to break out of a loop: http://docs.python.org/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops
</li>
<li>
Making decisions with <code>if</code>, <code>elif</code>, and <code>else</code>: http://docs.python.org/tutorial/controlflow.html#if-statements
</li>
</ul>
 
 
=== 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; in this case, print an error message saying they need to supply a file, and then exit the program using the <code>exit()</code> function.
 
<b>Step 5 resources</b>:
Line 91 ⟶ 130:
</li>
</ul>
 
 
===Checking your work===
Line 96 ⟶ 136:
Try out your script on the following quiz files:
 
* http://web.mit.edu/jesstess/www/IntermediatePythonWorkshop/metric.txt
* http://web.mit.edu/jesstess/www/IntermediatePythonWorkshop/french_food.txt
 
Does your script handle the case where the user forgets to provide a filename?
 
 
===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 to help people study. Keep practicing!
 
[[File:Fireworks.png|150px]]
[[File:Balloons.png|150px]]
Anonymous user