|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +# Data |
| 4 | +donors_and_donations = ['Steve Jobs', [5, 10, 15], 'Bill Gates', [ |
| 5 | + 30, 40, 50], 'Barak Obama', [50], 'Donald Trump', [ |
| 6 | + 1, 2, 3], 'Melinda Gates', [10, 20, 30]] |
| 7 | +# |
| 8 | +# “Send a Thank You”, “Create a Report” or “quit” |
| 9 | +# |
| 10 | + |
| 11 | + |
| 12 | +def create_a_report(): |
| 13 | + """ Print out a report of donors, total given number of donations |
| 14 | + and average donation |
| 15 | + """ |
| 16 | + donor_name = [] |
| 17 | + donor_total_given = [] |
| 18 | + donor_total_num_of_gifts = [] |
| 19 | + total_given = 0.0 |
| 20 | + average_gift = [] |
| 21 | + list_of_donations = [] |
| 22 | + average_of_gifts = 0 |
| 23 | + print('{:<20}|{:^15}|{:^15}|{:^15}'.format( |
| 24 | + 'Donor Name', 'Total Given', 'Num Gifts', 'Average Gift')) |
| 25 | + print('-' * 65) |
| 26 | + # Separate the individual donors and donations into separate lists |
| 27 | + i = 0 |
| 28 | + while i < len(donors_and_donations): |
| 29 | + donor_name.append(donors_and_donations[i]) |
| 30 | + list_of_donations.append(donors_and_donations[i + 1]) |
| 31 | + i += 2 |
| 32 | + # Calculate the sum of the gifts, the number of gifts, avg gift amount |
| 33 | + # for each donor, (each in a separate list) |
| 34 | + for donations in list_of_donations: |
| 35 | + # print(donations) |
| 36 | + # print(len(donations)) |
| 37 | + i = 0 |
| 38 | + total_given = 0 |
| 39 | + # calculate the total given |
| 40 | + while i < len(donations): |
| 41 | + # print(donations[i]) |
| 42 | + total_given += donations[i] |
| 43 | + i += 1 |
| 44 | + average_of_gifts = total_given / len(donations) |
| 45 | + average_gift.append(average_of_gifts) |
| 46 | + donor_total_given.append(total_given) |
| 47 | + donor_total_num_of_gifts.append(len(donations)) |
| 48 | + i = 0 |
| 49 | + while i < len(donors_and_donations) // 2: |
| 50 | + print('{:<20}${:^15}${:^15}${:8.2f}'.format( |
| 51 | + donor_name[i], donor_total_given[i], donor_total_num_of_gifts[i], |
| 52 | + average_gift[i])) |
| 53 | + i += 1 |
| 54 | + |
| 55 | + |
| 56 | +def compose_email(answer, answer1): |
| 57 | + """ Generate a one line email with donor name and amount contributed |
| 58 | + """ |
| 59 | + print('Thank you, {} for your generous donation of {}'.format |
| 60 | + (answer, answer1)) |
| 61 | + |
| 62 | + |
| 63 | +def print_menu(): |
| 64 | + """Prompt the user to choose from a menu of 3 actions, |
| 65 | + 'Send a Thank You', 'Create a Report' or 'quit' |
| 66 | + """ |
| 67 | + print("Menu\n1. Send a Thank You\n2. Create a Report\n3. Quit") |
| 68 | + answer = input("Enter a number between 1 and 3 (e.g 3) > ") |
| 69 | + answer = answer.strip() |
| 70 | + return answer |
| 71 | + |
| 72 | + |
| 73 | +def menu_thank_you(): |
| 74 | + """Prompt the user for a full list of donors or to update the current list |
| 75 | + """ |
| 76 | + # print(donors_and_donations) |
| 77 | + answer = input('Enter the full name of the donor or enter "list" for \ |
| 78 | +the list of donors, or "quit" for main menu') |
| 79 | + return answer |
| 80 | + |
| 81 | + |
| 82 | +def send_a_thank_you(): |
| 83 | + """Update the donor list with donor name and amount donated. |
| 84 | + or print the list out |
| 85 | + """ |
| 86 | + answer = menu_thank_you() |
| 87 | + answer = answer.strip() |
| 88 | + answer1 = 0 |
| 89 | + while True: |
| 90 | + if answer.lower() == 'list': |
| 91 | + print("The list of donors is: ") |
| 92 | + print(donors_and_donations[::2]) |
| 93 | + answer = menu_thank_you() |
| 94 | + elif answer.lower() == 'quit': |
| 95 | + break |
| 96 | + # If donor is in the list, add the donation |
| 97 | + else: |
| 98 | + i = 0 |
| 99 | + flag = 0 |
| 100 | + for x in donors_and_donations: |
| 101 | + if x == answer: |
| 102 | + print('answer is ', answer) |
| 103 | + flag = 1 |
| 104 | + answer1 = int( |
| 105 | + input("Enter the amount {} donated".format(x))) |
| 106 | + donors_and_donations[i + 1].append(answer1) |
| 107 | + print('donors_and_donations =', donors_and_donations) |
| 108 | + i += 1 |
| 109 | + # donor not in the list |
| 110 | + if flag == 0: |
| 111 | + answer1 = int( |
| 112 | + input("Enter the amount {} donated".format(answer))) |
| 113 | + donors_and_donations.append(answer) |
| 114 | + donors_and_donations.append([answer1]) |
| 115 | + compose_email(answer, answer1) |
| 116 | + answer = menu_thank_you() |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == '__main__': |
| 120 | + answer = print_menu() |
| 121 | + while True: |
| 122 | + if answer == '3': |
| 123 | + break |
| 124 | + elif answer == '1': |
| 125 | + send_a_thank_you() |
| 126 | + answer = print_menu() |
| 127 | + elif answer == '2': |
| 128 | + create_a_report() |
| 129 | + answer = print_menu() |
| 130 | + else: |
| 131 | + print("You entered {}".format(answer)) |
| 132 | + print("Menu\n1. Send a Thank You\n2. Create a Report\n3. Quit") |
| 133 | + answer = input("Enter a number between 1 and 3 (e.g 3) > ") |
0 commit comments