|
| 1 | +#! usr/bin/env python3 |
| 2 | +""" |
| 3 | +mail room refractored! |
| 4 | +""" |
| 5 | +import math |
| 6 | +import unittest |
| 7 | + |
| 8 | +donors_dict = { |
| 9 | + 'Hermione Granger': [43.01, 13.13], |
| 10 | + 'Molly Weasley':[87.03, 44.55], |
| 11 | + 'Luna Lovegood':[61.03, 44.87, 27.77], |
| 12 | + 'Sybill Trelawney':[77.56, 33.45, 756.32] |
| 13 | + } |
| 14 | + |
| 15 | +# main menu functions |
| 16 | +def main_menu(): |
| 17 | + action = input(''' |
| 18 | + Thank you donating to Hogwarts School. Choose an action: |
| 19 | + 1. - Send a Thank You Owl |
| 20 | + 2. - Create a Report |
| 21 | + 3. - Test |
| 22 | + 4. - Quit |
| 23 | + > ''') |
| 24 | + return action.strip() |
| 25 | + |
| 26 | +def write_ty(donor_input, amount): |
| 27 | + return(''' |
| 28 | + Dear {} |
| 29 | + Thank you for your very donation of ${:.2f}. |
| 30 | + You have enabled excellent magical instruction! |
| 31 | + Sincerely, |
| 32 | + -Hogwarts School of Witchcraft and Wizardy |
| 33 | + '''.format(donor_input, amount)) |
| 34 | + |
| 35 | +def send_owl(): |
| 36 | + while True: |
| 37 | + donor_name = input(''' |
| 38 | + Enter a donor's name. |
| 39 | + *or* |
| 40 | + Enter 'list' to view donor list |
| 41 | + Enter 'return' to return to main menu |
| 42 | +
|
| 43 | + > |
| 44 | + ''').strip() |
| 45 | + |
| 46 | + if donor_name == 'list': |
| 47 | + print_donors_list() |
| 48 | + elif donor_name == 'return': |
| 49 | + return |
| 50 | + else: |
| 51 | + break |
| 52 | + |
| 53 | + while True: |
| 54 | + amount = input(''' |
| 55 | + Enter donation amount |
| 56 | + > $ |
| 57 | + ''').strip() |
| 58 | + if amount == "return": |
| 59 | + return |
| 60 | + try: |
| 61 | + amount = float(amount) |
| 62 | + except ValueError: |
| 63 | + print("Invali entry! please enter a number!") |
| 64 | + else: |
| 65 | + break |
| 66 | + |
| 67 | + donors_dict[donor_name].append(amount) |
| 68 | + print(write_ty(donor_name, amount)) |
| 69 | + print(send_owl()) |
| 70 | + return |
| 71 | + |
| 72 | +def print_donors_list(): |
| 73 | + print("Donors: \n ") |
| 74 | + for key in donors_dict: |
| 75 | + print(key) |
| 76 | + |
| 77 | +def sort_key(item): |
| 78 | + return item[1] |
| 79 | + |
| 80 | +def print_report(): |
| 81 | + report_rows = [] |
| 82 | + |
| 83 | + for keys, values in donors_dict.items(): |
| 84 | + total_gifts = sum(values) |
| 85 | + num_gifts = len(values) |
| 86 | + avg_gift = total_gifts / num_gifts |
| 87 | + report_rows.append((keys, total_gifts, num_gifts, avg_gift)) |
| 88 | + |
| 89 | + report_rows.sort(key=sort_key) |
| 90 | + |
| 91 | + # print table with spaces for formatting |
| 92 | + print("{:30s} {:10s} {:10s} {:10s}".format( |
| 93 | + "Donor Name", "Total Given", "Num Gifts", "Average Gift")) |
| 94 | + print("*" * 66) |
| 95 | + rows = [print("{:30s} {:10f} {:10d} {:10f} ".format(*row)) for row in report_rows] |
| 96 | + |
| 97 | +def quit(): |
| 98 | + sys.exit(0) |
| 99 | + |
| 100 | +def test(): |
| 101 | + unittest.main() |
| 102 | + return |
| 103 | + |
| 104 | +class MyTests(unittest.TestCase): |
| 105 | + def test_abc(self): |
| 106 | + selection = ("1") |
| 107 | + donor_input = "Helena" |
| 108 | + amount = 55.66 |
| 109 | + pass |
| 110 | + |
| 111 | + |
| 112 | +# dictionary for the menu!! |
| 113 | +if __name__ == "__main__": |
| 114 | + running_dict = { |
| 115 | + "1":send_owl, |
| 116 | + "2":print_report, |
| 117 | + "3":test, |
| 118 | + "4":quit |
| 119 | + } |
| 120 | + selection = main_menu() |
| 121 | + try: |
| 122 | + running_dict.get(selection)() |
| 123 | + except (KeyError): |
| 124 | + print("This error again!") |
0 commit comments