Flash-card-Challenge

Python script that takes a file containing flash card questions and answers
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.
This commit is contained in:
Utkarsh Sharma 2018-10-02 15:03:23 -07:00
parent e9406f42ff
commit 265d3c9eb7
6 changed files with 326 additions and 0 deletions

View File

@ -0,0 +1,121 @@
# 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 containing flash card questions and answers 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
Download: http://web.mit.edu/jesstess/www/IntermediatePythonWorkshop/state_capitals.txt
Write the code to open and read state_capitals.txt (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 remove from the word.
Step 1 resources:
* File I/O: 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.
To randomly select a key from the dictionary, you can use the random module, and in
particular the random.choice function.
When you run your script, to break out of the while loop you can press Control and then
(while still holding down Control) c.
Step 2 resources:
* While loops: http://en.wikibooks.org/wiki/Python_Programming/Flow_control#While_loops
* Dictionary manipulation: http://docs.python.org/tutorial/datastructures.html#dictionaries
In particular, look at getting a list of the dictionary's keys using the keys method.
* Selecting a random value from a list using the random module: http://docs.python.org/library/random.html#random.choice
### 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.
You can get input from a user using the raw_input function.
It is up to you how strict you want to be with a user's answer. Do you want capitalization
to matter?
Step 3 resources:
* using raw_input to get data from the user: http://docs.python.org/library/functions.html#raw_input
### 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 4 resources:
* Using the break keyword to break out of a loop: http://docs.python.org/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops
* Making decisions with if, elif, and else: http://docs.python.org/tutorial/controlflow.html#if-statements
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 exit() function.
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
<FINISHED>

View File

@ -0,0 +1,91 @@
Project
Write a flash card quizzer from scratch
Goals:
1. practice breaking down a problem and solving it in Python from scratch
2. practice command line option parsing
3. practice reading from files
4. practice working with dictionaries and for loops
Problem statement:
Write a Python script that takes a file containing flash card questions and answers 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.
Breaking down the problem
Step 1: Get the questions from a fixed flash card file
Download: http://web.mit.edu/jesstess/www/IntermediatePythonWorkshop/state_capitals.txt
Write the code to open and read state_capitals.txt (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 remove from the word.
Step 1 resources:
1. File I/O: http://docs.python.org/tutorial/inputoutput.html#reading-and-writing-files
2. 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.
To randomly select a key from the dictionary, you can use the random module, and in
particular the random.choice function.
When you run your script, to break out of the while loop you can press Control and then
(while still holding down Control) c.
Step 2 resources:
1. while loops: http://en.wikibooks.org/wiki/Python_Programming/Flow_control#While_loops
2. Dictionary manipulation: http://docs.python.org/tutorial/datastructures.html#dictionaries
In particular, look at getting a list of the dictionary's keys using the keys method.
3. Selecting a random value from a list using the random module: http://docs.python.org/library/random.html#random.choice
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.
You can get input from a user using the raw_input function.
It is up to you how strict you want to be with a user's answer. Do you want capitalization
to matter?
Step 3 resources:
1. using raw_input to get data from the user: http://docs.python.org/library/functions.html#raw_input
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 4 resources:
1. Using the break keyword to break out of a loop: http://docs.python.org/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops
2. Making decisions with if, elif, and else: http://docs.python.org/tutorial/controlflow.html#if-statements
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 exit() function.
Step 5 resources:
1. Command line argument parsing: http://docs.python.org/library/argparse.html#module-argparse
2. Getting and checking the number of command line arguments: http://docs.python.org/library/sys.html

View File

@ -0,0 +1,26 @@
la nourriture,food
avoir faim,to be hungry
manger,to eat
le repas,meal
le petit-déjeuner,breakfast
le déjeuner,lunch
le dîner,dinner
le goûter,snack
le hors d'Å“uvre,appetizer
le plat principal,main course
la salle à manger,dining room
la confiture,jam
le croissant,croissant
la farine,flour
les frites,French fries
l'huile d'olive,olive oil
la mayonnaise,mayonnaise
la moutarde,mustard
un Å“uf,egg
le pain,bread
le pain grillé,toast
les pâtes,pasta
le poivre,pepper
le riz,rice
le sel,salt
le sucre,suga

View File

@ -0,0 +1,14 @@
peta,quadrillion (10^15)
tera,trillion (10^12)
giga,billion (10^9)
mega,million (10^6)
kilo,thousand (10^3)
hecto,hundred (10^2)
deka,ten (10^1)
deci,tenth (10^-1)
centi,hundredth (10^-2)
milli,thousandth (10^-3)
micro,millionth (10^-6)
nano,billionth (10^-9)
pico,trillionth (10^-12)
femto,quadrillionth (10^-15)

View File

@ -0,0 +1,24 @@
import random
import argparse
parser = argparse.ArgumentParser(description="Get the quiz questions file")
parser.add_argument('file', help="a quiz file containing questions and answers")
args = parser.parse_args()
file = args.file
state_capitals = {}
with open(file) as f:
for line in f:
(key, val) = line.strip().split(',')
state_capitals[key] = val
while(True):
choice = random.choice(list(state_capitals.keys()))
answer = input(('{}? '.format(choice)))
if answer == state_capitals[choice]:
print("Correct! Nice job.")
elif answer.lower() == "exit":
print("Goodbye")
break
else:
print("Incorrect. The correct answer is {}".format(state_capitals[choice]))

View File

@ -0,0 +1,50 @@
Alabama,Montgomery
Alaska,Juneau
Arizona,Phoenix
Arkansas,Little Rock
California,Sacramento
Colorado,Denver
Connecticut,Hartford
Delaware,Dover
Florida,Tallahassee
Georgia,Atlanta
Hawaii,Honolulu
Idaho,Boise
Illinois,Springfield
Indiana,Indianapolis
Iowa,Des Moines
Kansas,Topeka
Kentucky,Frankfort
Louisiana,Baton Rouge
Maine,Augusta
Maryland,Annapolis
Massachusetts,Boston
Michigan,Lansing
Minnesota,Saint Paul
Mississippi,Jackson
Missouri,Jefferson City
Montana,Helena
Nebraska,Lincoln
Nevada,Carson City
New Hampshire,Concord
New Jersey,Trenton
New Mexico,Santa Fe
New York,Albany
North Carolina,Raleigh
North Dakota,Bismarck
Ohio,Columbus
Oklahoma,Oklahoma City
Oregon,Salem
Pennsylvania,Harrisburg
Rhode Island,Providence
South Carolina,Columbia
South Dakota,Pierre
Tennessee,Nashville
Texas,Austin
Utah,Salt Lake City
Vermont,Montpelier
Virginia,Richmond
Washington,Olympia
West Virginia,Charleston
Wisconsin,Madison
Wyoming,Cheyenne