Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Students/Hui Zhang/session03/mailroom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from mailroomfunction import sendletter
from mailroomfunction import printout

Userlist = ['Jerry', [10, 20, 30], 'Peter', [5, 6], 'Aly', [3, 4], 'Anisha', [11, 15, 13], 'Chris', [9, 11, 14], 'Vini', [13, 17, 16]]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will work, but probably better to explicitly group the person and the donation list, as I talked about in class:

UserList = [ ( 'Jerry', [10, 20, 20] ), ....

That way you don't need to do a bunch of index manipulations to keep track.

So maybe update with a dict for the next week.


input1 = input("To send a Thanks You letter, press '1'\nTo create a report, press '2'\nTo Exit, press '0' \n")

while input1:
if input1 == 1:
sendletter(Userlist, input1)
input1 = input("To send a Thanks You letter, press '1'\nTo create a report, press '2'\nTo Exit, press '0' \n")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you want to try to structure your while loops so you don't need to repeat the input message.usually a while True: at teh top, then the input message, and break out when you want to end it.

elif input1 == 2:
printout(Userlist, input1)
input1 = input("To send a Thanks You letter, press '1'\nTo Create a report, press '2'\nTo Exit, press '0' \n")
55 changes: 55 additions & 0 deletions Students/Hui Zhang/session03/mailroomfunction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@


def sendletter(Userlist, input1):
# Print out all existing donators name.
print Userlist[::2]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a slick way to get the names -- isn't slicing handy?

username1 = raw_input("Please type a name from above existing Donator List or enter a new name: ")
# Verify if input are all digis.
amount1 = raw_input("Please input the amount of your donation: ")
# Determine if your input is digi.
while not amount1.isdigit():
print "Error, your input is not a number!!!"
amount1 = raw_input("Please input the amount of your donation: ")
# Determine if the donator name just typed in is actually in the list.
# If it is in the list, then do the below:
if username1 in Userlist:
locationuser1 = Userlist.index(username1)
Userlist[locationuser1 + 1].append(int(amount1))
# If it is not in the list, then append it in the list.
else:
Userlist.append(username1)
Userlist.append([int(amount1)])
print '\n'
print "Thank you, " + username1 + ", for your generous donation of amount of: $" + amount1 + " Dollars ! \n\n"
return

if __name__ == "__main__":
# execute only if run as a script
main()

def printout(Userlist, input1):
#initilize a list named donator_tuples
donator_tuples = []
# create a list wiht donator name, sum of donation, times of donation and average donation.
for username2 in Userlist[::2]:
locationuser2 = Userlist.index(username2)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is where grouping the user and donations would help. This works pretty well, but is not very efficient, the code is searching through the whole user list multiple times. Not a big deal with a small data set, but would kill you with a big one.

amount2 = sum(Userlist[locationuser2 + 1])
# calculate how many times of donation
length2 = len(Userlist[locationuser2 + 1])
# calculate average donation
average2 = int(amount2 / length2)
donator_tuples.append([username2, amount2, length2, average2])
# sort the list: donator_tuples by sum of donation.
donator_tuples_sorted = sorted(donator_tuples, key=lambda donator: donator[1])

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice use of sorted and lambda!

print '\n\n'
# Left align strings to be printed out
print '{0:<10} {1:<8} {2:<8} {3:<8}'.format("Donator", "Total", "Times", "Average")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

very nice formatting

for username3 in donator_tuples_sorted:
# Left align strings/numbers in the sorted list: donator_tuples_sorted to be printed out
print '{0:<10s} ${1:<7d} {2:<8d} ${3:<8d}'.format(username3[0], username3[1], username3[2], username3[3])
print '\n'
return

if __name__ == "__main__":
# execute only if run as a script
main()