You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Write a Python program called `gashlycrumb.py` that takes a letter of the alphabet as an argument and looks up the line in a `-f|--file` argument (default `gashlycrumb.txt`) and prints the line starting with that letter.
3
+
Write a Python program called `gashlycrumb.py` that takes a letter of the alphabet as an argument and looks up the line in a `-f|--file` argument (default `gashlycrumb.txt`) and prints the line starting with that letter. It should generate usage with no arguments or for `-h|--help`:
You can see the structure of the default "gashlycrumb.txt" file:
23
+
24
+
````
25
+
$ head -3 gashlycrumb.txt
26
+
A is for Amy who fell down the stairs.
27
+
B is for Basil assaulted by bears.
28
+
C is for Clara who wasted away.
29
+
````
30
+
31
+
You will use the first character of the line as a lookup value:
32
+
33
+
````
24
34
$ ./gashlycrumb.py a
25
35
A is for Amy who fell down the stairs.
26
36
$ ./gashlycrumb.py z
27
37
Z is for Zillah who drank too much gin.
28
38
````
29
39
30
-
If you are not familiar with the work of Edward Gorey, please stop and go read about him immediately, e.g. https://www.brainpickings.org/2011/01/19/edward-gorey-the-gashlycrumb-tinies/!
40
+
If given a value that does not exist (when searched with regard to case), you should print a message:
31
41
32
-
Write your own version of Gorey's text and pass in your version as the `--file`.
42
+
````
43
+
$ ./gashlycrumb.py 3
44
+
I do not know "3".
45
+
````
33
46
34
-
Write an interactive version that takes input directly from the user:
47
+
You expect the positional argument to be exactly one character long. If it is not, exit with an error:
35
48
36
49
````
37
-
$ ./gashlycrumb_i.py
38
-
Please provide a letter [! to quit]: a
39
-
A is for Amy who fell down the stairs.
40
-
Please provide a letter [! to quit]: b
41
-
B is for Basil assaulted by bears.
42
-
Please provide a letter [! to quit]: !
43
-
Bye
50
+
$ ./gashlycrumb.py CH
51
+
"CH" is not 1 character.
44
52
````
53
+
54
+
If you provide a `--file` argument that does not exist, you should exit with an error and message:
55
+
56
+
````
57
+
$ ./gashlycrumb.py -f sdfl b
58
+
usage: gashlycrumb.py [-h] [-f str] str
59
+
gashlycrumb.py: error: argument -f/--file: can't open 'sdfl': \
60
+
[Errno 2] No such file or directory: 'sdfl'
61
+
````
62
+
63
+
For those last two, look into using `argparse.FileType('r')` to describe the `type` of the `--file` argument so that `argparse` will do the check and create the error. For the length of the `letter` argument, look into using `parser.error`.
If you haven't yet explored dictionaries, this is a natural solution.
2
+
3
+
## Edward Gorey
4
+
5
+
If you are not familiar with the work of Edward Gorey, please stop and go read about him immediately, e.g. https://www.brainpickings.org/2011/01/19/edward-gorey-the-gashlycrumb-tinies/!
6
+
7
+
## Alternate text
8
+
9
+
Write your own version of Gorey's text and pass in your version as the `--file`. I include my own `alternate.txt` which I used the simple and Soundex rhymers to help me find words.
10
+
11
+
## Interactive version
12
+
13
+
Write an interactive version that takes input directly from the user:
0 commit comments