-
Notifications
You must be signed in to change notification settings - Fork 76
Adding session04 Dict_Set Lab #46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
b20a49f
7b22806
f365ce4
e58ae11
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| #Create a data structure with 2 elements (Name, Dollar Amount Donated) | ||
|
|
||
| donor_db = [] | ||
| donor_db.append( ("Jimmy Page", [653772.32, 12.17]) ) | ||
| donor_db.append( ("Robert Plant", [877.33]) ) | ||
| donor_db.append( ("Roger Daltry", [663.23, 43.87, 1.32]) ) | ||
| donor_db.append( ("Pete Townsend", [1663.23, 4300.87, 10432.0]) ) | ||
| donor_db.append( ("Mick Jagger", [653772.32, 12.17]) ) | ||
|
|
||
| def write_thk_letrs(user_input2): | ||
| #Write a letter thanking a donor# | ||
|
|
||
| print ("Dear %s. \n\nThank you for your generous contribution !\n\nYou Rock !\n\n" % (user_input2)) | ||
| return main() | ||
|
|
||
|
|
||
| def thanks_note(): | ||
| user_input2 =raw_input('Please provide the full name of the donor, or type "list for a list of donors" :\n\n') | ||
|
|
||
| if user_input2 == 'list': | ||
| tmp_list1= [] | ||
| tmp_list2= [] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why the temp list at all? you can just loop through eh main list and print name as you go. |
||
| i = "" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here -- it get re-bound in the for loop anyway. |
||
| tmp_list1 = donor_list[0::2] | ||
| for i in tmp_list1: | ||
| if i not in tmp_list2: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is htis checking for duplicates? if so, probably better for database integrity to be done elsewhere -- and no need for this assignment anyway. |
||
| tmp_list2.append(i) | ||
| for x in tmp_list2: | ||
| print ("\n" + x + "\n") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. print adds a newline -- doesn't this make a lot of blank space? |
||
| return add_donor() | ||
|
|
||
| else: | ||
| for d_name in donor_list: | ||
| if d_name == user_input2: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this work? I think it should be: if d_name[0] == user_input2: |
||
| return write_thk_letrs(user_input2) | ||
|
|
||
|
|
||
| def add_donor(): | ||
| user_input2 =raw_input('Please provide the full name of the donor, or type "list for a list of donors" :\n\n') | ||
| for d_name in donor_list: | ||
|
|
||
| if d_name != user_input2: | ||
| break | ||
|
|
||
| while True: | ||
| user_input3 = raw_input("\n" + user_input2 + " is a new donor,and can be added to the donor list.\n " " \nWould you like" | ||
| " to make a donation for " + user_input2 + " ?\n\nPlease answer 'Yes' or 'No' ") | ||
|
|
||
| if user_input3 == "No": | ||
| main() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. careful -- this gets you in a recursive loop -- main() calls add_donor, which calls main(), which calls add_donor, ..... this could get ugly in a long running process. This should probably be a return() |
||
|
|
||
| if user_input3 == "Yes": | ||
| break | ||
|
|
||
| while True: | ||
| user_input4 = raw_input('Enter the donation amount \n\n ') | ||
|
|
||
| #if user_input3 == 'menu': | ||
| #return 'menu' | ||
| try: | ||
| user_input4 = float(user_input4) | ||
| break | ||
|
|
||
| except ValueError: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice! |
||
| print 'Specified Donations must be a in a numeric format, renter amount.' | ||
|
|
||
|
|
||
| user_input4 = "$"+str(user_input4) | ||
| donor_list.extend([user_input2, user_input4]) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this match your current data structure? |
||
| write_thk_letrs(user_input2) | ||
|
|
||
| def donor_report(): | ||
|
|
||
| tmp_list3= [] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. again, no need to pre-declare... |
||
| tmp_list4= [] | ||
| user_input5 ="" | ||
| i = "" | ||
| tmp_list3 = donor_list[0::2] | ||
|
|
||
| print("\n\nDonor Name Total Amount Donated Number of Donations Average Donation\n" ) | ||
| for i in tmp_list3: | ||
| if i not in tmp_list4: | ||
| tmp_list4.append(i) | ||
| for x in tmp_list4: | ||
| print ("\n" + x + "\n") | ||
|
|
||
| def main(): | ||
|
|
||
|
|
||
| while True: | ||
| user_input1 = '' | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need to pre-declare these, either. |
||
| user_input2 = '' | ||
| user_input3 = '' | ||
| user_input4 = '' | ||
| user_input4 = '' | ||
|
|
||
| user_input1 = raw_input('Welcome to the Donations Support Program\n\n' | ||
| 'Please enter a 1 to Send a Thank You Note \n\n' | ||
| 'Please enter a 2 to Create a Report \n\n' | ||
| 'Please enter a 3 to exit this project \n\n') | ||
|
|
||
|
|
||
| if user_input1 == '1': | ||
| #print("Send a Thank You Note") | ||
| return thanks_note() | ||
|
|
||
|
|
||
| if user_input1 == '2': | ||
| #print("Create a Report") | ||
| return donor_report() | ||
|
|
||
|
|
||
| if user_input1 == '3': | ||
| print ("Exiting this Program") | ||
| break | ||
|
|
||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| main() | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| #Create a dictionary | ||
| dict_1 = {} | ||
| dict_1={'name':'Chris','city':'Seattle','cake':'Chocolate'} | ||
|
|
||
| #Display the dictionary | ||
| dict_1 | ||
|
|
||
| #Delete the entry for �cake�. | ||
| dict_1.pop('cake') | ||
|
|
||
| #Add an entry for �fruit� with �Mango� and display the dictionary. | ||
| dict_1.setdefault('fruit', 'Mango') | ||
| #Display the dictionary keys | ||
| dict_1.keys() | ||
|
|
||
| #Display the dictionary values | ||
| dict_1.keys() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. dict_1.values() ? |
||
|
|
||
| #Display whether or not �cake� is a key in the dictionary (i.e. False) (now). | ||
| 'cake'in dict_1 | ||
|
|
||
| #Display whether or not �Mango� is a value in the dictionary (i.e. True). | ||
| 'Mango'in dict_1.values() | ||
|
|
||
| #Using the dict constructor and zip, build a dictionary of numbers | ||
| #from zero to fifteen and the hexadecimal equivalent (string is fine). | ||
|
|
||
| def gen_lists(): | ||
|
|
||
| llist1 =[] | ||
| hlist1 =[] | ||
| n_list =[] | ||
| tmp_lists_dict ={} | ||
|
|
||
| llist1 = range(1,16) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is created here, so no need to pre-declare |
||
| for i in llist1: | ||
| x = hex(i) | ||
| hlist1.append(x) | ||
|
|
||
| n_list = zip(llist1,hlist1) | ||
| #return (n_list) | ||
| tmp_lists_dict = dict(n_list) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. also here -- a new one is being created -- no need to have pre-declared it. |
||
| print (tmp_lists_dict) | ||
|
|
||
|
|
||
| gen_lists() | ||
|
|
||
|
|
||
| #Using the dictionary from item 1: Make a dictionary using the same keys but | ||
| #with the number of�t�s in each value. | ||
|
|
||
| dict_1={'name':'Chris','city':'Seattle','cake':'Chocolate'} | ||
| dict_2={} | ||
| list_1=[] | ||
| for a,b in dict_1.items(): | ||
| list_1.append((a,b.count('t'))) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not build up the dict directly here? This works fine, but not much point. |
||
| dict_2 =dict(list_1) | ||
| print dict_2 | ||
|
|
||
|
|
||
| #Create sets set_2, set_3 and set_4 that contain numbers from zero through twenty, divisible 2, 3 and 4. | ||
|
|
||
|
|
||
| s_0 = set(range(21)) | ||
| set_2 = [] | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. careful! these are lists, not sets! try set_1 = set() then set__1.add(i) |
||
| set_3 = [] | ||
| set_4 = [] | ||
| for i in s_0: | ||
| if i % 2 == 0: | ||
| set_2.append(i) | ||
| set_2 = set(set_2) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why make the list first? |
||
|
|
||
| for i in s_0: | ||
| if i % 3 == 0: | ||
| set_3.append(i) | ||
| set_3 = set(set_3) | ||
|
|
||
| for i in s_0: | ||
| if i % 4 == 0: | ||
| set_4.append(i) | ||
| set_4 = set(set_4) | ||
|
|
||
|
|
||
| #Display the sets. | ||
| print set_1 | ||
| print set_2 | ||
| print set_3 | ||
| print set_4 | ||
|
|
||
| #Display if set_3 is a subset of set_2 (False) | ||
|
|
||
| set(set_3).issubset(set_2) | ||
|
|
||
| #if set_4 is a subset of set_2 (True) | ||
| set(set_4).issubset(set_2) | ||
|
|
||
|
|
||
| # Create a set with the letters in �Python� and add �i� to the set. | ||
| x = set("Python") | ||
| x.add("i") | ||
|
|
||
| # Create a frozenset with the letters in �marathon� | ||
| y = frozenset(�marathon�) | ||
|
|
||
| # display the union and intersection of the two sets. | ||
| x.union(y) | ||
| x.intersection(y) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| """Creating Wrapper Function Safe Input """ | ||
| def safe_input(msg): | ||
| """Function is called when ^C or ^D occurs""" | ||
| try: | ||
| x = raw_input(user_action) | ||
| except (EOFError, KeyboardInterrupt): | ||
| return None | ||
| else: | ||
| return variaxble | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what this? did you mean return x? |
||
| if __name__ == "__main__": | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need to pre-define this -- it'll get crated later later anyway.