Flash card challenge: Difference between revisions

From OpenHatch wiki
Content added Content deleted
imported>Jesstess
(Created page with "== 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 p...")
 
imported>Jesstess
No edit summary
Line 44: Line 44:
Goodbye</pre>
Goodbye</pre>


== Project steps ==
== Breaking down the problem ==


=== Step 1: Get the questions from a fixed flash card file==
=== 1. Learn about HSV values ===


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.
Run the ColorWall effects again with


<b>Step 1 resources</b>:
<pre>python run.py -a</pre>
<ul>
<li>
File input and output: http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files.
</li>
<li>
Stripping characters (like whitespace and newlines) from a string: http://docs.python.org/library/stdtypes.html#str.strip.
</li>
</ul>


The names of the effects are printed to the terminal as they are run. Pay particular attention to the first 4 effects:
* SolidColorTest
* HueTest
* SaturationTest
* ValueTest


=== Step 2: Randomly select questions from the question dictionary ===
In all of these effects, a tuple <code>hsv</code> containing the hue, saturation, and value describing a color are passed to <code>self.wall.set_pixel</code> to change the color of a single pixel on the wall.


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.
What are the differences between these tests? Given these difference and how they are expressed visually, how does varying hue, saturation, or value change a color?


<b>Check your understanding</b>: what saturation and value would you guess firetruck red have?


=== Step 3: Get and check the user's answer ===


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.
=== 2. Examine <code>Effect</code> and the interface its subclasses provide ===


All of the effects inherit from the <code>Effect</code> class. Examine this class and its <code>__init__</code> and <code>run</code> methods.


=== Step 4: Allow the user to quit the program ===
What is the purpose of the <code>__init__</code> method?


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.
What is the purpose of the <code>run</code> method?


Open up <code>run.py</code> and look at this chunk of code at the bottom of the file:


=== Step 5: Get the quiz questions file ===
<pre>
for effect in effects_to_run:
new_effect = effect(wall)
print new_effect.__class__.__name__
new_effect.run()
</pre>


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.
<code>effects.py</code> exports and <code>Effects</code> list at the bottom of the file. <code>run.py</code> goes through every effect in that list, creates a new instance of the effect, and invokes its <code>run</code> method.


<b>Step 2 resources</b>:
<b>Check your understanding</b>: what would happen if you added an effect to the <code>Effects</code> list that didn't implement a <code>run</code> method? (Try it!)
<ul>
<li>
Command line argument parsing: http://docs.python.org/library/argparse.html#module-argparse.
</li>
<li>
Getting and checking the number of command line arguments: http://docs.python.org/library/sys.html.
</li>
</ul>




===Checking your work===


What happens when you run your script on the following inputs?
=== 3. Examine the nested <code>for</code> loop in <code>SolidColorTest</code> ===

<pre>for x in range(self.wall.width):
for y in range(self.wall.height):
self.wall.set_pixel(x, y, hsv)</pre>

This code loops over every pixel in the ColorWall, setting the pixel to a particular <code>hsv</code> value. After that <code>for</code> loop is over, <code>self.wall.draw()</code> updates the display.

<b>Check your understanding</b>: what would happen if you moved the <code>self.wall.draw()</code> to inside the inner <code>for</code> loop, just under <code>self.wall.set_pixel(x, y, hsv)</code> in <code>SaturationTest</code>? (Try it!)

<b>Tip</b>: you can run individual tests by passing their names as command line arguments to <code>run.py</code>. For example, if you only wanted to run <code>SaturationTest</code>, you could:

<pre>python run.py SaturationTest</pre>


=== 4. Implement a new effect called <code>RainbowTest</code> ===

It should run for 5 seconds, cycling through the colors in the rainbow, pausing for a moment at each color.

Remember to add your effect to the <code>Effect</code> list at the bottom of <code>effects.py</code>!

Test your new effect with

<pre>python run.py RainbowTest</pre>


=== 5. Play with the randomness in <code>Twinkle</code> ===

Walk through <code>Twinkle</code>. Find explanations of the <code>random.randint</code> and <code>random.uniform</code> functions in the online documentation at http://docs.python.org/library/random.html.

Experiment with these functions at a Python prompt:


<pre>
<pre>
$ python scrabble.py
import random
Usage: scrabble.py [RACK]
random.randint(0, 1)
random.randint(0, 5)
random.uniform(-1, 1)
</pre>
</pre>

Then experiment with the numbers that make up the hue and re-run the effect:


<pre>
<pre>
python run.py Twinkle
$ python scrabble.py AAAaaaa
2 aa
</pre>
</pre>

<b>Challenge</b>: make <code>Twinkle</code> twinkle with shades of red.


=== 6. Implement a new effect that involves randomness! ===

Remember to add your effect to the <code>Effect</code> list at the bottom of <code>effects.py</code>.

==Bonus exercises==

===Checkerboard===

Find and change the colors used in the <code>Checkerboards</code> effect, and re-run the effect:

<pre>
python run.py Checkerboards
</pre>

Then change the line

<pre>
if (x + y + i) % 2 == 0:
</pre>

to

<pre>
if (x + y + i) % 3 == 0:
</pre>

re-run the effect, and see what changed.

What other patterns can you create by tweaking the math for this effect?


=== Matrix ===

Find and change the color of the columns in the <code>Matrix</code> effect, and re-run the effect:


<pre>
<pre>
python run.py Matrix
$ 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>
</pre>


===Bonus challenge===
Each column that we see on the wall corresponds to a <code>Column</code> object. Add some randomness to the color used by each column (the variable whose value you changed above) using the <code>random.random</code> function, re-run the effect, and see what happens.


Modify your script to handle blank tiles. Blank tiles have a score of 0 but can be used to represent any letter.


===Congratulations!===
=== Write more of your own effects! ===


You've implemented a substantial, useful script in Python from scratch. Keep practicing!
You have color, time, randomness, letters, and more at your disposal. Go nuts!

Revision as of 20:03, 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:


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

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 2 resources:


Checking your work

What happens when you run your script on the following inputs?

$ python scrabble.py 
Usage: scrabble.py [RACK]
$ python scrabble.py AAAaaaa
2 aa
$ 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

Bonus challenge

Modify your script to handle blank tiles. Blank tiles have a score of 0 but can be used to represent any letter.

Congratulations!

You've implemented a substantial, useful script in Python from scratch. Keep practicing!