-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment5.py
More file actions
72 lines (49 loc) · 2.15 KB
/
assignment5.py
File metadata and controls
72 lines (49 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python3
class myDictionary:
workshopDict = {"names": [], "gender": []}
def __init__(self):
print("Creating a Dictionary")
# the keyName is the key you are adding to
# the newVal is the item you want to add to the list
def addVal(self, keyName, newVal):
"""to complete this method use what you learned to add an item to a dictionary along with the append method for a list, then print the dictionary"""
self.workshopDict.update({keyName : newVal})
for (key, value) in self.workshopDict.items():
print(key, " :: ", value)
pass
# the newKey parameter is the name of the new key to be added
# the valueType parameter of this key will default to an empty list
def addKey(self, newKey, valueType):
"""To complete this method use what you learned about adding a key to the dictionary, then print the dictionary. """
self.workshopDict[newKey] = valueType
for (key, value) in self.workshopDict.items():
print(key, " :: ", value)
pass
# keyToDRemove is the name of the key to remove
def deleteKey(self, keyToRemove):
"""to complete this method use what we learned to remove a key from a dictionary, then print the dictionary"""
del self.workshopDict[keyToRemove]
for (key, value) in self.workshopDict.items():
print(key, " :: ", value)
pass
def main():
print("initializing the myDictionary class to call in main")
thisDict = myDictionary()
print("adding names and genders to the list, in the dictionary")
thisDict.addVal("names", "Abigail")
thisDict.addVal("names", "Bryan")
thisDict.addVal("names", "Glen")
thisDict.addVal("names", "Mary")
thisDict.addVal("gender", "female")
thisDict.addVal("gender", "male")
thisDict.addVal("gender", "male")
thisDict.addVal("gender", "female")
print("adding a new key to the dictionary with a default value type of an empty list")
thisDict.addKey("age", [])
print("adding values to the list associated with the new key that was added")
thisDict.addVal("age", 33)
thisDict.addVal("age", 21)
print("removing the specified key from the dictionary")
thisDict.deleteKey("gender")
if __name__ == "__main__":
main()