Skip to content

Commit 7febcf0

Browse files
committed
Merge pull request UWPCE-PythonCert#46 from PrimusPilus/master
Adding session04 Dict_Set Lab, auto-merging
2 parents 819e43c + e58ae11 commit 7febcf0

3 files changed

Lines changed: 240 additions & 0 deletions

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#Create a data structure with 2 elements (Name, Dollar Amount Donated)
2+
3+
donor_db = []
4+
donor_db.append( ("Jimmy Page", [653772.32, 12.17]) )
5+
donor_db.append( ("Robert Plant", [877.33]) )
6+
donor_db.append( ("Roger Daltry", [663.23, 43.87, 1.32]) )
7+
donor_db.append( ("Pete Townsend", [1663.23, 4300.87, 10432.0]) )
8+
donor_db.append( ("Mick Jagger", [653772.32, 12.17]) )
9+
10+
def write_thk_letrs(user_input2):
11+
#Write a letter thanking a donor#
12+
13+
print ("Dear %s. \n\nThank you for your generous contribution !\n\nYou Rock !\n\n" % (user_input2))
14+
return main()
15+
16+
17+
def thanks_note():
18+
user_input2 =raw_input('Please provide the full name of the donor, or type "list for a list of donors" :\n\n')
19+
20+
if user_input2 == 'list':
21+
tmp_list1= []
22+
tmp_list2= []
23+
i = ""
24+
tmp_list1 = donor_list[0::2]
25+
for i in tmp_list1:
26+
if i not in tmp_list2:
27+
tmp_list2.append(i)
28+
for x in tmp_list2:
29+
print ("\n" + x + "\n")
30+
return add_donor()
31+
32+
else:
33+
for d_name in donor_list:
34+
if d_name == user_input2:
35+
return write_thk_letrs(user_input2)
36+
37+
38+
def add_donor():
39+
user_input2 =raw_input('Please provide the full name of the donor, or type "list for a list of donors" :\n\n')
40+
for d_name in donor_list:
41+
42+
if d_name != user_input2:
43+
break
44+
45+
while True:
46+
user_input3 = raw_input("\n" + user_input2 + " is a new donor,and can be added to the donor list.\n " " \nWould you like"
47+
" to make a donation for " + user_input2 + " ?\n\nPlease answer 'Yes' or 'No' ")
48+
49+
if user_input3 == "No":
50+
main()
51+
52+
if user_input3 == "Yes":
53+
break
54+
55+
while True:
56+
user_input4 = raw_input('Enter the donation amount \n\n ')
57+
58+
#if user_input3 == 'menu':
59+
#return 'menu'
60+
try:
61+
user_input4 = float(user_input4)
62+
break
63+
64+
except ValueError:
65+
print 'Specified Donations must be a in a numeric format, renter amount.'
66+
67+
68+
user_input4 = "$"+str(user_input4)
69+
donor_list.extend([user_input2, user_input4])
70+
write_thk_letrs(user_input2)
71+
72+
def donor_report():
73+
74+
tmp_list3= []
75+
tmp_list4= []
76+
user_input5 =""
77+
i = ""
78+
tmp_list3 = donor_list[0::2]
79+
80+
print("\n\nDonor Name Total Amount Donated Number of Donations Average Donation\n" )
81+
for i in tmp_list3:
82+
if i not in tmp_list4:
83+
tmp_list4.append(i)
84+
for x in tmp_list4:
85+
print ("\n" + x + "\n")
86+
87+
def main():
88+
89+
90+
while True:
91+
user_input1 = ''
92+
user_input2 = ''
93+
user_input3 = ''
94+
user_input4 = ''
95+
user_input4 = ''
96+
97+
user_input1 = raw_input('Welcome to the Donations Support Program\n\n'
98+
'Please enter a 1 to Send a Thank You Note \n\n'
99+
'Please enter a 2 to Create a Report \n\n'
100+
'Please enter a 3 to exit this project \n\n')
101+
102+
103+
if user_input1 == '1':
104+
#print("Send a Thank You Note")
105+
return thanks_note()
106+
107+
108+
if user_input1 == '2':
109+
#print("Create a Report")
110+
return donor_report()
111+
112+
113+
if user_input1 == '3':
114+
print ("Exiting this Program")
115+
break
116+
117+
118+
119+
if __name__ == '__main__':
120+
main()
121+
122+
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#Create a dictionary
2+
dict_1 = {}
3+
dict_1={'name':'Chris','city':'Seattle','cake':'Chocolate'}
4+
5+
#Display the dictionary
6+
dict_1
7+
8+
#Delete the entry for “cake”.
9+
dict_1.pop('cake')
10+
11+
#Add an entry for “fruit” with “Mango” and display the dictionary.
12+
dict_1.setdefault('fruit', 'Mango')
13+
#Display the dictionary keys
14+
dict_1.keys()
15+
16+
#Display the dictionary values
17+
dict_1.keys()
18+
19+
#Display whether or not “cake” is a key in the dictionary (i.e. False) (now).
20+
'cake'in dict_1
21+
22+
#Display whether or not “Mango” is a value in the dictionary (i.e. True).
23+
'Mango'in dict_1.values()
24+
25+
#Using the dict constructor and zip, build a dictionary of numbers
26+
#from zero to fifteen and the hexadecimal equivalent (string is fine).
27+
28+
def gen_lists():
29+
30+
llist1 =[]
31+
hlist1 =[]
32+
n_list =[]
33+
tmp_lists_dict ={}
34+
35+
llist1 = range(1,16)
36+
for i in llist1:
37+
x = hex(i)
38+
hlist1.append(x)
39+
40+
n_list = zip(llist1,hlist1)
41+
#return (n_list)
42+
tmp_lists_dict = dict(n_list)
43+
print (tmp_lists_dict)
44+
45+
46+
gen_lists()
47+
48+
49+
#Using the dictionary from item 1: Make a dictionary using the same keys but
50+
#with the number of‘t’s in each value.
51+
52+
dict_1={'name':'Chris','city':'Seattle','cake':'Chocolate'}
53+
dict_2={}
54+
list_1=[]
55+
for a,b in dict_1.items():
56+
list_1.append((a,b.count('t')))
57+
dict_2 =dict(list_1)
58+
print dict_2
59+
60+
61+
#Create sets set_2, set_3 and set_4 that contain numbers from zero through twenty, divisible 2, 3 and 4.
62+
63+
64+
s_0 = set(range(21))
65+
set_2 = []
66+
set_3 = []
67+
set_4 = []
68+
for i in s_0:
69+
if i % 2 == 0:
70+
set_2.append(i)
71+
set_2 = set(set_2)
72+
73+
for i in s_0:
74+
if i % 3 == 0:
75+
set_3.append(i)
76+
set_3 = set(set_3)
77+
78+
for i in s_0:
79+
if i % 4 == 0:
80+
set_4.append(i)
81+
set_4 = set(set_4)
82+
83+
84+
#Display the sets.
85+
print set_1
86+
print set_2
87+
print set_3
88+
print set_4
89+
90+
#Display if set_3 is a subset of set_2 (False)
91+
92+
set(set_3).issubset(set_2)
93+
94+
#if set_4 is a subset of set_2 (True)
95+
set(set_4).issubset(set_2)
96+
97+
98+
# Create a set with the letters in ‘Python’ and add ‘i’ to the set.
99+
x = set("Python")
100+
x.add("i")
101+
102+
# Create a frozenset with the letters in ‘marathon’
103+
y = frozenset(“marathon”)
104+
105+
# display the union and intersection of the two sets.
106+
x.union(y)
107+
x.intersection(y)
108+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""Creating Wrapper Function Safe Input """
2+
def safe_input(msg):
3+
"""Function is called when ^C or ^D occurs"""
4+
try:
5+
x = raw_input(user_action)
6+
except (EOFError, KeyboardInterrupt):
7+
return None
8+
else:
9+
return variaxble
10+
if __name__ == "__main__":

0 commit comments

Comments
 (0)