Skip to content

Commit c689032

Browse files
committed
updated mailroom2 solution
1 parent d088f58 commit c689032

File tree

1 file changed

+53
-45
lines changed

1 file changed

+53
-45
lines changed

source/solutions/Lesson04/mailroom2.py

Lines changed: 53 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ def list_donors():
4545
return "\n".join(listing)
4646

4747

48+
def print_donor_list():
49+
"""
50+
Doesn't do much, but keeps the printing separate
51+
"""
52+
print(list_donors())
53+
print()
54+
55+
4856
def find_donor(name):
4957
"""
5058
Find a donor in the donor db
@@ -68,10 +76,6 @@ def add_donor(name):
6876
donor_db[name.lower()] = donor
6977
return donor
7078

71-
72-
73-
74-
7579
def gen_letter(donor):
7680
"""
7781
Generate a thank you letter for the donor
@@ -92,35 +96,35 @@ def gen_letter(donor):
9296
'''.format(donor[0], donor[1][-1]))
9397

9498

95-
def take_donation(name):
99+
def take_donation():
96100
"""
97101
Ask user for donation amount, and then add it to the DB
98102
"""
99103
# Now prompt the user for a donation amount to apply. Since this is
100104
# also an exit point to the main menu, we want to make sure this is
101105
# done before mutating the db.
106+
print("in take_donation")
107+
name = input("Enter a donor name (new or existing): \n >")
102108
while True:
103-
amount_str = input("Enter a donation amount (or 'menu' to exit)> ").strip()
104-
if amount_str == "menu":
109+
amount_str = input("Enter a donation amount (or <enter> to exit)> ").strip()
110+
if not amount_str:
111+
# if they provide no input, go back to previous menu
105112
return
106113
# Make sure amount is a valid amount before leaving the input loop
107-
try:
108-
amount = float(amount_str)
109-
# extra check here -- unlikely that someone will type "NaN", but
110-
# it IS possible, and it is a valid floating point number:
111-
# http://en.wikipedia.org/wiki/NaN
112-
if math.isnan(amount) or math.isinf(amount) or round(amount, 2) == 0.00:
113-
raise ValueError
114-
# in this case, the ValueError could be raised by the float() call, or by the NaN-check
115-
except ValueError:
114+
amount = float(amount_str)
115+
# extra check here -- unlikely that someone will type "NaN", but
116+
# it IS possible, and it is a valid floating point number:
117+
# http://en.wikipedia.org/wiki/NaN
118+
if math.isnan(amount) or math.isinf(amount) or round(amount, 2) == 0.00:
116119
print("error: donation amount is invalid\n")
120+
continue
117121
else:
118122
break
119123

120-
# If this is a new user, ensure that the database has the necessary
121-
# data structure.
122124
donor = find_donor(name)
125+
# If the donor is not found, it's a new donor
123126
if donor is None:
127+
# add the new donor to the database
124128
donor = add_donor(name)
125129

126130
# Record the donation
@@ -129,23 +133,6 @@ def take_donation(name):
129133
print(gen_letter(donor))
130134

131135

132-
def send_thank_you():
133-
"""
134-
Execute the logic to record a donation and generate a thank you message.
135-
"""
136-
# Read a valid donor to send a thank you from, handling special commands to
137-
# let the user navigate as defined.
138-
while True:
139-
name = input("Enter a donor's name or 'list' to see all donors or "
140-
"'menu' to exit to main menu > ").strip()
141-
if name == "list":
142-
print(list_donors())
143-
elif name == "menu":
144-
return
145-
else:
146-
take_donation(name)
147-
148-
149136
def sort_key(item):
150137
# used to sort on name in donor_db
151138
return item[1]
@@ -194,6 +181,10 @@ def print_donor_report():
194181
print(generate_donor_report())
195182

196183

184+
def return_to_menu():
185+
''' Return True to trigger exit out of sub-loop'''
186+
return True
187+
197188
def quit():
198189
"""
199190
quit the program
@@ -203,6 +194,21 @@ def quit():
203194
"""
204195
sys.exit(0)
205196

197+
def send_thank_you():
198+
"""
199+
Execute the logic to record a donation and generate a thank you message.
200+
"""
201+
# Read a valid donor to send a thank you from, handling special commands to
202+
# let the user navigate as defined.
203+
prompt = ("To send a thank you, select one:\n\n"
204+
"(1) Update donor and send thank-you\n"
205+
"(2) List all existing DONORS\n"
206+
"(3) Return to main menu\n > ")
207+
selection_dict = {"1": take_donation,
208+
"2": print_donor_list,
209+
"3": return_to_menu,
210+
}
211+
run_menu(prompt, selection_dict)
206212

207213
def main_menu():
208214
"""
@@ -211,10 +217,10 @@ def main_menu():
211217
prompt = dedent('''
212218
Choose an action:
213219
214-
1 - Send a Thank You
215-
2 - Create a Report
216-
3 - Send letters to everyone
217-
4 - Quit
220+
(1) - Send a Thank You
221+
(2) - Create a Report
222+
(3) - Send letters to everyone
223+
(4) - Quit
218224
219225
> ''')
220226

@@ -236,13 +242,15 @@ def run_menu(prompt, selection_dict):
236242
the actions to take.
237243
"""
238244
while True:
239-
240-
selection = input(prompt).strip()
241-
try:
242-
# This calls teh function in the selection_dict
243-
selection_dict[selection]()
244-
except KeyError:
245+
selection = input(prompt).strip().lower()
246+
action = selection_dict.get(selection, None)
247+
if action is None:
245248
print("error: menu selection is invalid!")
249+
else:
250+
if action():
251+
# break out of the loop if action returns True
252+
break
253+
246254

247255
if __name__ == "__main__":
248256

0 commit comments

Comments
 (0)