|
7 | 7 | check input |
8 | 8 | """ |
9 | 9 |
|
| 10 | +import sys |
| 11 | +import math |
| 12 | + |
10 | 13 | # handy utility to make pretty printing easier |
11 | 14 | from textwrap import dedent |
12 | 15 |
|
13 | | -import math |
14 | 16 |
|
15 | 17 | # In memory representation of the donor database |
16 | 18 | # using a tuple for each donor |
@@ -86,14 +88,14 @@ def gen_letter(donor): |
86 | 88 |
|
87 | 89 | :returns: string with letter |
88 | 90 | """ |
89 | | - return dedent('''Dear %s |
| 91 | + return dedent('''Dear {0:s} |
90 | 92 |
|
91 | | - Thank you for your very kind donation of $%.2f. |
| 93 | + Thank you for your very kind donation of ${1:.2f}. |
92 | 94 | It will be put to very good use. |
93 | 95 |
|
94 | 96 | Sincerely, |
95 | 97 | -The Team |
96 | | - ''' % (donor[0], donor[1][-1]) ) |
| 98 | + '''.format(donor[0], donor[1][-1]) ) |
97 | 99 |
|
98 | 100 |
|
99 | 101 | def send_thank_you(): |
@@ -182,17 +184,27 @@ def save_letters_to_disk(): |
182 | 184 | open(filename, 'w').write(letter) |
183 | 185 |
|
184 | 186 |
|
| 187 | +def print_donor_report(): |
| 188 | + print generate_donor_report() |
| 189 | + |
| 190 | + |
| 191 | +def quit(): |
| 192 | + sys.exit(0) |
| 193 | + |
185 | 194 | if __name__ == "__main__": |
186 | 195 | running = True |
187 | | - while running: |
| 196 | + |
| 197 | + selection_dict = {"1": send_thank_you, |
| 198 | + "2": print_donor_report, |
| 199 | + "3": save_letters_to_disk, |
| 200 | + "4": quit} |
| 201 | + |
| 202 | + while True: |
188 | 203 | selection = main_menu_selection() |
189 | | - if selection is "1": |
190 | | - send_thank_you() |
191 | | - elif selection is "2": |
192 | | - print generate_donor_report() |
193 | | - elif selection is "3": |
194 | | - save_letters_to_disk() |
195 | | - elif selection is "4": |
196 | | - running = False |
197 | | - else: |
| 204 | + try: |
| 205 | + selection_dict[selection]() |
| 206 | + except KeyError: |
198 | 207 | print "error: menu selection is invalid!" |
| 208 | + |
| 209 | + |
| 210 | + |
0 commit comments