Flash card challenge: Difference between revisions

no edit summary
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:
Goodbye</pre>
 
== ProjectBreaking stepsdown 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>
$ python scrabble.py
import random
Usage: scrabble.py [RACK]
random.randint(0, 1)
random.randint(0, 5)
random.uniform(-1, 1)
</pre>
 
Then experiment with the numbers that make up the hue and re-run the effect:
 
<pre>
$ python runscrabble.py TwinkleAAAaaaa
2 aa
</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>
$ python runscrabble.py MatrixZZAAEEI
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===
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!
Anonymous user