|
| 1 | + |
| 2 | + |
| 3 | +def sendletter(Userlist, input1): |
| 4 | + # Print out all existing donators name. |
| 5 | + print Userlist[::2] |
| 6 | + username1 = raw_input("Please type a name from above existing Donator List or enter a new name: ") |
| 7 | + # Verify if input are all digis. |
| 8 | + amount1 = raw_input("Please input the amount of your donation: ") |
| 9 | + # Determine if your input is digi. |
| 10 | + while not amount1.isdigit(): |
| 11 | + print "Error, your input is not a number!!!" |
| 12 | + amount1 = raw_input("Please input the amount of your donation: ") |
| 13 | + # Determine if the donator name just typed in is actually in the list. |
| 14 | + # If it is in the list, then do the below: |
| 15 | + if username1 in Userlist: |
| 16 | + locationuser1 = Userlist.index(username1) |
| 17 | + Userlist[locationuser1 + 1].append(int(amount1)) |
| 18 | + # If it is not in the list, then append it in the list. |
| 19 | + else: |
| 20 | + Userlist.append(username1) |
| 21 | + Userlist.append([int(amount1)]) |
| 22 | + print '\n' |
| 23 | + print "Thank you, " + username1 + ", for your generous donation of amount of: $" + amount1 + " Dollars ! \n\n" |
| 24 | + return |
| 25 | + |
| 26 | + if __name__ == "__main__": |
| 27 | + # execute only if run as a script |
| 28 | + main() |
| 29 | + |
| 30 | +def printout(Userlist, input1): |
| 31 | + #initilize a list named donator_tuples |
| 32 | + donator_tuples = [] |
| 33 | + # create a list wiht donator name, sum of donation, times of donation and average donation. |
| 34 | + for username2 in Userlist[::2]: |
| 35 | + locationuser2 = Userlist.index(username2) |
| 36 | + amount2 = sum(Userlist[locationuser2 + 1]) |
| 37 | + # calculate how many times of donation |
| 38 | + length2 = len(Userlist[locationuser2 + 1]) |
| 39 | + # calculate average donation |
| 40 | + average2 = int(amount2 / length2) |
| 41 | + donator_tuples.append([username2, amount2, length2, average2]) |
| 42 | + # sort the list: donator_tuples by sum of donation. |
| 43 | + donator_tuples_sorted = sorted(donator_tuples, key=lambda donator: donator[1]) |
| 44 | + print '\n\n' |
| 45 | + # Left align strings to be printed out |
| 46 | + print '{0:<10} {1:<8} {2:<8} {3:<8}'.format("Donator", "Total", "Times", "Average") |
| 47 | + for username3 in donator_tuples_sorted: |
| 48 | + # Left align strings/numbers in the sorted list: donator_tuples_sorted to be printed out |
| 49 | + print '{0:<10s} ${1:<7d} {2:<8d} ${3:<8d}'.format(username3[0], username3[1], username3[2], username3[3]) |
| 50 | + print '\n' |
| 51 | + return |
| 52 | + |
| 53 | + if __name__ == "__main__": |
| 54 | + # execute only if run as a script |
| 55 | + main() |
0 commit comments