|
1 | 1 | """ |
2 | 2 | #-------------------------------------------------# |
3 | | -# Title: Mailroom - Week3 assignment |
| 3 | +# Title: Mailroom - Week3/4 assignment |
4 | 4 | # Dev: BBounds |
5 | | -# Date: January 28 2016 |
| 5 | +# Date: February 1 2016 |
6 | 6 | #Class: Python 2016a |
7 | 7 | Instructor: Rick Riehle / TA: Summer Rae |
8 | 8 | #-------------------------------------------------# |
|
13 | 13 |
|
14 | 14 | It should have a data structure that holds a list of your donors and a history of the amounts |
15 | 15 | they have donated. This structure should be populated at first with at least five donors, with |
| 16 | +
|
| 17 | +
|
16 | 18 | between 1 and 3 donations each |
17 | 19 | The script should prompt the user (you) to choose from a menu of 2 actions: |
18 | | - ‘Send a Thank You’ or ‘Create a Report’. |
| 20 | + Send a Thank You or Create a Report. |
19 | 21 |
|
20 | | -Sending a Thank You |
21 | | -If the user (you) selects ‘Send a Thank You’, prompt for a Full Name. |
22 | | -If the user types ‘list’, show them a list of the donor names and re-prompt |
| 22 | +
|
| 23 | +If the user (you) selects "Send a Thank You", prompt for a Full Name. |
| 24 | +If the user types "list", show them a list of the donor names and re-prompt |
23 | 25 | If the user types a name not in the list, add that name to the data structure and use it. |
24 | 26 | If the user types a name in the list, use it. |
| 27 | +
|
25 | 28 | Once a name has been selected, prompt for a donation amount. |
26 | | -Verify that the amount is in fact a number, and re-prompt if it isn’t. |
| 29 | +Verify that the amount is in fact a number, and re-prompt if it isnt. |
27 | 30 | Once an amount has been given, add that amount to the donation history of the selected user. |
| 31 | +
|
28 | 32 | Finally, use string formatting to compose an email thanking the donor for their generous donation. |
29 | 33 | Print the email to the terminal and return to the original prompt. |
30 | 34 | It is fine to forget new donors once the script quits running. |
31 | 35 |
|
32 | 36 | Creating a Report |
33 | | -If the user (you) selected ‘Create a Report’ Print a list of your donors, sorted by total historical donation amount. |
| 37 | +If the user (you) selected "Create a Report" Print a list of your donors, sorted by total historical donation amount. |
34 | 38 | Include Donor Name, total donated, number of donations and average donation amount as values in each row. |
35 | 39 | Using string formatting, format the output rows as nicely as possible. The end result should be tabular |
36 | 40 | (values in each column should align with those above and below) |
37 | 41 | After printing this report, return to the original prompt. |
38 | | -At any point, the user should be able to quit their current task and return to the original prompt. |
| 42 | +At any point, the user should be able to quit their current task and return to the original prompt. ### NEED HELP HERE |
39 | 43 | From the original prompt, the user should be able to quit the script cleanly |
| 44 | +
|
| 45 | +
|
40 | 46 | """ |
| 47 | +# memo to run the file only if this is a main file |
41 | 48 |
|
42 | 49 | if __name__ == "__main__": |
43 | | - print("hi there") |
| 50 | + print("hi there - this is a main file and can run this script directly") |
44 | 51 | else: |
45 | 52 | raise Exception("This file was not created to be imported") |
| 53 | +# set up imaginary donor information for use in the script |
| 54 | +dictall ={} |
| 55 | + |
| 56 | + |
| 57 | +# input made-up values into a dictionary - very inefficeint - better to pull from a file (but had problems) |
| 58 | + |
| 59 | +d1 = {("Sally", "Wood", "Ms."): [50.00, 100.45, 75.25]} |
| 60 | +d2 = {("Jim", "Nasium", "Mr."): [150.00, 10.00]} |
| 61 | +d3 = {("Bill", "Fold", "Mr."): [45.00]} |
| 62 | +d4 = {("Alice", "Wonder", "Mrs."): [10.00, 10.00, 25.00]} |
| 63 | +d5 = {("Chuck", "Wheels", "Mr."): [25.00, 25,25 ]} |
| 64 | +d6 = {("no", "one", "yet"): [] } |
| 65 | +dictnamelist = ["d1","d2","d3","d4","d5","d6"] |
| 66 | +dictall.update(d1) |
| 67 | +dictall.update(d2) |
| 68 | +dictall.update(d3) |
| 69 | +dictall.update(d4) |
| 70 | +dictall.update(d5) |
| 71 | +dictall.update(d6) |
| 72 | + |
| 73 | +print ("dictall = ",dictall) |
| 74 | + |
| 75 | + |
| 76 | +# for fun, unpack the key and make a new key in a new dictionary with re-ordered values (like in a letter) |
| 77 | +dictsalutation = {} |
| 78 | + |
| 79 | +def print_report(): |
| 80 | + |
| 81 | + srtdictall = {} |
| 82 | + #srtdictall = sorted(dictall,key= sum(list(dictall.values()))) *** need to figure out sort by value |
| 83 | + print("\n\t Donor's Report: \n\t _________________\n") |
| 84 | + for tplKey, lstValue in dictall.items(): |
| 85 | + donations = 0 |
| 86 | + count = 0 |
| 87 | + #print(strKey + " / " + strValue + "\t") |
| 88 | + #print("type ofkey",type(strKey)) |
| 89 | + firstn = tplKey[0] |
| 90 | + lastn = tplKey[1] |
| 91 | + saluten = tplKey[2] |
| 92 | + nameall = saluten + " " + firstn + " " + lastn |
| 93 | + dictsalutation[nameall] = lstValue |
| 94 | + #print( "Dear", nameall) |
| 95 | + #print("type of value",type(lstValue)) |
| 96 | + donations = sum(lstValue) |
| 97 | + count = (len(lstValue)) |
| 98 | + try: |
| 99 | + avg_donation = donations/count |
| 100 | + except ZeroDivisionError: |
| 101 | + avg_donation = 0 |
| 102 | + |
| 103 | + except Exception as e: |
| 104 | + print(e) |
| 105 | + |
| 106 | + print("{} {} has made {:d} donation(s) for a total of ${:f}" |
| 107 | + "which is an ave of {:f}".format(firstn,lastn,count,donations,avg_donation)) |
| 108 | + |
| 109 | + input("\n\ttype any key to continue") |
| 110 | + |
| 111 | +print(dictall) |
| 112 | +print("*****") |
| 113 | +print(dictsalutation) |
| 114 | +print("***** - now sorted") |
| 115 | +print(sorted(dictsalutation)) |
| 116 | +print("***** - now back to unsorted - is it still there?") |
| 117 | +print(dictsalutation) |
| 118 | + |
| 119 | + |
| 120 | + |
| 121 | +''' |
| 122 | +def problem_user(): |
| 123 | + print("you stink. ") |
| 124 | + return |
| 125 | +
|
| 126 | +#def verify(a, count = 0): |
| 127 | + like = "no" |
| 128 | + print("You entered: ", a) |
| 129 | + like = input("Is this correct? Hit 'enter'' for yes or type 'no'").lower |
| 130 | + if like == "" or like == "yes": |
| 131 | + return a |
| 132 | + else: |
| 133 | + new_a = input("please input your data again: ") |
| 134 | + count +=1 |
| 135 | + if count == 3: |
| 136 | + print("you seem to be having problems.") |
| 137 | + problem_user() |
| 138 | + verify(new_a) |
| 139 | +''' |
| 140 | + |
| 141 | +def print_thanks(donors_fullname): |
| 142 | + print("Thank you ",donors_fullname) |
| 143 | + print("need to format a thank ou letter") |
| 144 | + |
| 145 | +def add_new_donor(): |
| 146 | + donor_fname = input("What is the new donor's first name?").title |
| 147 | + f = (donor_fname) |
| 148 | + donor_lastn = input("What is the new donor's last name?").title |
| 149 | + l = (donor_lastn) |
| 150 | + donor_salutation = input("What is the donor's salutation for a letter? i.e: Mr. or Mrs?").title |
| 151 | + mr =(donor_salutation) |
| 152 | + key = (f , l , mr) |
| 153 | + print("the type of key created to dictionary is: ", type(key), "is" , key) |
| 154 | + return (key) |
| 155 | + |
| 156 | +def add_new_donor_to_dictionary(unposted_new_donor): |
| 157 | + if unposted_new_donor in dictall: |
| 158 | + print("name already in dictionary.") |
| 159 | + return |
| 160 | + else: |
| 161 | + dictall[unposted_new_donor] = [] #update the dictionary with a new key with empty list value |
| 162 | + dictsalutation[unposted_new_donor] = [] #update the salutation dictionary too (with new key with empty list value |
| 163 | + message = "Completed set up of new donor (added to dictionary):" |
| 164 | + print ("{} {}".format(message,unposted_new_donor)) |
| 165 | + return unposted_new_donor |
| 166 | + |
| 167 | + |
| 168 | +"""Once a name has been selected, prompt for a donation amount. |
| 169 | +Verify that the amount is in fact a number, and re-prompt if it isnt. |
| 170 | +Once an amount has been given, add that amount to the donation history of the selected user. |
| 171 | +""" |
| 172 | + |
| 173 | +def add_new_donations(key_name): |
| 174 | + new_donation_Q= input("Has {} provided a new donation? Type 'yes' or 'no'".format(key_name)).lower |
| 175 | + a= verify(new_donation_Q) |
| 176 | + if key_name in dictall: |
| 177 | + |
| 178 | + lstValueNew =[] |
| 179 | + if a == "yes": |
| 180 | + donation_amount = int(input("What is the amount of the donation?")) |
| 181 | + try: |
| 182 | + b = int(verify(new_donation_Q)) |
| 183 | + except Exception as e: |
| 184 | + print(e, "is not a valid number. value not excepted" ) |
| 185 | + return None |
| 186 | + lstValueNew.append(b) |
| 187 | + |
| 188 | + dictall[key_name] = lstValue |
| 189 | + dictsalutation[key_name] = lstValue |
| 190 | + print("Ive updated that: Donor {} provided {:d} in a donation").format(key_name,lstValue) |
| 191 | + else: |
| 192 | + print("name {} not found. Try again.". format(key_name)) |
| 193 | + |
| 194 | + return |
| 195 | + |
| 196 | +def list_donors(): |
| 197 | + ''' screen print list of donors in a table''' |
| 198 | + print("temp# \t donor name \t\t donation history \n " |
| 199 | + "--------------------------------------------------------") |
| 200 | + count = 0 |
| 201 | + for tplkey,lstvalue in sorted(dictsalutation.items()): |
| 202 | + print ("No: {} \t {} \t\t {}".format((count+1),tplkey,lstvalue),sep="\t\t") |
| 203 | + count += 1 |
| 204 | + input("\n\ttype enter to proceed") |
| 205 | + |
| 206 | +# create a menu |
| 207 | +while(True): |
| 208 | + print (""" |
| 209 | + Menu of Options: |
| 210 | + ----------------- |
| 211 | + 1) Send a Thank You letter |
| 212 | + 2) Add a new Donor |
| 213 | + 3) Add new Donations |
| 214 | + 4) List Donors |
| 215 | + 5) Create a Report |
| 216 | + 6) Exit |
| 217 | +
|
| 218 | + """) |
| 219 | + strChoice = str(input("Which option would you like to perform? [1 to 6]")) |
| 220 | + |
| 221 | + # 1 send thank you letter |
| 222 | + |
| 223 | + ''' |
| 224 | + If the user (you) selects "Send a Thank You", prompt for a Full Name. |
| 225 | +If the user types "list", show them a list of the donor names and re-prompt |
| 226 | +If the user types a name not in the list, add that name to the data structure and use it. |
| 227 | +If the user types a name in the list, use it. |
| 228 | +''' |
| 229 | + if (strChoice == '1'): |
| 230 | + full_name = input("Please enter the:" |
| 231 | + "1)full name and title of the donor, or" |
| 232 | + "2)'list' to see a list of donors, or" |
| 233 | + "3)'exit' to quit ").title |
| 234 | + if full_name == "exit": |
| 235 | + break |
| 236 | + elif full_name == "list": |
| 237 | + list_donors() |
| 238 | + continue |
| 239 | + |
| 240 | + elif full_name in dictsalutation: |
| 241 | + print("found it in dictsalutation") |
| 242 | + print_thanks(full_name) |
| 243 | + continue |
| 244 | + else: |
| 245 | + #********* |
| 246 | + print("I do not see this name in the list.") |
| 247 | + full_name = verify(full_name) |
| 248 | + answer = input("Would you like to try again or add {} name to the list?".format(full_name)) |
| 249 | + if answer == "try again": |
| 250 | + break |
| 251 | + else: |
| 252 | + print("Ok, We will add {} to the list. Lets collect the details one at a time.".format(full_name)) |
| 253 | + created_new_donor_tpl = add_new_donor() |
| 254 | + add_new_donations(created_new_donor_tpl) |
| 255 | + print_thanks(created_new_donor_tpl) |
| 256 | + |
| 257 | + |
| 258 | + |
| 259 | + # 2 add new donor |
| 260 | + elif(strChoice == '2'): |
| 261 | + print("please check that the donor is not already on the list") |
| 262 | + list_donors() |
| 263 | + answer = input("do you still want to add the new donor? Type yes or no") |
| 264 | + a = (answer) |
| 265 | + if a == 'yes': |
| 266 | + add_new_donor() |
| 267 | + continue |
| 268 | + |
| 269 | + |
| 270 | + #3 add new donation |
| 271 | + elif(strChoice == '3'): |
| 272 | + print("please find your donor on the list below") |
| 273 | + list_donors() |
| 274 | + keynametry = input("please type the donors full name") |
| 275 | + add_new_donations(keynametry) |
| 276 | + continue |
| 277 | + |
| 278 | + |
| 279 | + #4 list donors |
| 280 | + elif(strChoice == '4'): |
| 281 | + print("here is a list of donors") |
| 282 | + list_donors() |
| 283 | + continue |
| 284 | + |
| 285 | + #5 create a report |
| 286 | + elif(strChoice == '5'): |
| 287 | + print_report() |
| 288 | + continue |
| 289 | + |
| 290 | + |
| 291 | + #6 Quit |
| 292 | + elif(strChoice == '6'): |
| 293 | + print("Thank you. Have a good day.") |
| 294 | + break |
| 295 | + |
| 296 | + |
| 297 | + |
| 298 | +''' |
| 299 | +
|
| 300 | +
|
| 301 | +print (dictall) |
| 302 | +
|
| 303 | +
|
| 304 | +
|
| 305 | +
|
| 306 | +print (d5.keys()) |
| 307 | +print (d4.items()) |
| 308 | +print (d3.values()) |
| 309 | +
|
| 310 | +for strKey, strValue in dicTable.items(): |
| 311 | + print(strKey) |
| 312 | + strKeyToRemove = input("Which item would you like removed?") |
| 313 | + if(strKeyToRemove in dicTable): |
| 314 | + del dicTable[strKeyToRemove] |
| 315 | + else: |
| 316 | + print("I'm sorry, but I could not find that item.") |
| 317 | + print(dicTable) #For testing |
| 318 | + continue |
| 319 | +
|
46 | 320 |
|
| 321 | + elif(strChoice == '6'): |
| 322 | + objFile = open(objFileName, "w") |
| 323 | + for strKey, strValue in dicTable.items(): |
| 324 | + objFile.write(strKey + "," + strValue + "\n") |
| 325 | + objFile.close() |
| 326 | + break |
| 327 | +''' |
0 commit comments