Skip to content

Commit 6ef84dd

Browse files
committed
added dict to switch between menu items
1 parent a0862db commit 6ef84dd

1 file changed

Lines changed: 26 additions & 14 deletions

File tree

‎Solutions/Session04/mailroom.py‎

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
check input
88
"""
99

10+
import sys
11+
import math
12+
1013
# handy utility to make pretty printing easier
1114
from textwrap import dedent
1215

13-
import math
1416

1517
# In memory representation of the donor database
1618
# using a tuple for each donor
@@ -86,14 +88,14 @@ def gen_letter(donor):
8688
8789
:returns: string with letter
8890
"""
89-
return dedent('''Dear %s
91+
return dedent('''Dear {0:s}
9092
91-
Thank you for your very kind donation of $%.2f.
93+
Thank you for your very kind donation of ${1:.2f}.
9294
It will be put to very good use.
9395
9496
Sincerely,
9597
-The Team
96-
''' % (donor[0], donor[1][-1]) )
98+
'''.format(donor[0], donor[1][-1]) )
9799

98100

99101
def send_thank_you():
@@ -182,17 +184,27 @@ def save_letters_to_disk():
182184
open(filename, 'w').write(letter)
183185

184186

187+
def print_donor_report():
188+
print generate_donor_report()
189+
190+
191+
def quit():
192+
sys.exit(0)
193+
185194
if __name__ == "__main__":
186195
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:
188203
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:
198207
print "error: menu selection is invalid!"
208+
209+
210+

0 commit comments

Comments
 (0)