1
+ """
2
+ Again, the code is broken, you need to create 4 functions.
3
+ - add_to_dict: Add a word to a dict.
4
+ - get_from_dict: Get a word from inside a dict.
5
+ - update_word: Update a word inside of the dict.
6
+ - delete_from_dict: Delete a word from the dict.
7
+
8
+ All this functions should check for errors, follow the comments to see all cases you need to cover.
9
+
10
+ There should be NO ERRORS from Python in the console.
11
+ """
12
+
13
+ def add_to_dict (dict , * word ): #튜플 형태로 가변 매개변수 받기
14
+ if str (type (dict )) == "<class 'dict'>" :
15
+ if len (word ) == 2 :
16
+ if word [0 ] in dict :
17
+ print (f"{ word [0 ]} is already on the dictionary. Won't add." )
18
+ else :
19
+ dict [word [0 ]] = word [1 ]
20
+ print (f"{ word [0 ]} has been added." )
21
+ else :
22
+ print ("You need to send a word and a definition." )
23
+ else :
24
+ print (f"you need to send a dictionary. You sent: { type (dict )} " )
25
+
26
+ def get_from_dict (dict , * word ):
27
+ if str (type (dict )) == "<class 'dict'>" :
28
+ if len (word ) == 1 :
29
+ if word [0 ] in dict :
30
+ print (f"{ word [0 ]} : { dict [word [0 ]]} " )
31
+ else :
32
+ print (f"{ word [0 ]} was not found in this dict." )
33
+ else :
34
+ print ("You need to send a word to search for." )
35
+ else :
36
+ print (f"you need to send a dictionary. You sent: { type (dict )} " )
37
+
38
+ def update_word (dict , * word ):
39
+ if str (type (dict )) == "<class 'dict'>" :
40
+ if len (word ) == 2 :
41
+ if word [0 ] in dict :
42
+ dict [word [0 ]] = word [1 ]
43
+ print (f"{ word [0 ]} has been updated to: { word [1 ]} " )
44
+ else :
45
+ print (f"{ word [0 ]} is not on the dict. Can't update non-existing word." )
46
+ else :
47
+ print ("You need to send a word and a definition to update." )
48
+ else :
49
+ print (f"you need to send a dictionary. You sent: { type (dict )} " )
50
+
51
+ def delete_from_dict (dict , * word ):
52
+ if str (type (dict )) == "<class 'dict'>" :
53
+ if len (word ) == 1 :
54
+ if word [0 ] in dict :
55
+ del dict [word [0 ]]
56
+ print (f"{ word [0 ]} has been deleted." )
57
+ else :
58
+ print (f"{ word [0 ]} is not on in this dict. Won't delete." )
59
+ else :
60
+ print ("You need to specifty a word to delete." )
61
+ else :
62
+ print (f"you need to send a dictionary. You sent: { type (dict )} " )
63
+
64
+ # \/\/\/\/\/\/\ DO NOT TOUCH \/\/\/\/\/\/\
65
+
66
+ import os
67
+
68
+ os .system ('clear' )
69
+
70
+
71
+ my_english_dict = {}
72
+
73
+ print ("\n ###### add_to_dict ######\n " )
74
+
75
+ # Should not work. First argument should be a dict.
76
+ print ('add_to_dict("hello", "kimchi"):' )
77
+ add_to_dict ("hello" , "kimchi" )
78
+
79
+ # Should not work. Definition is required.
80
+ print ('\n add_to_dict(my_english_dict, "kimchi"):' )
81
+ add_to_dict (my_english_dict , "kimchi" )
82
+
83
+ # Should work.
84
+ print ('\n add_to_dict(my_english_dict, "kimchi", "The source of life."):' )
85
+ add_to_dict (my_english_dict , "kimchi" , "The source of life." )
86
+
87
+ # Should not work. kimchi is already on the dict
88
+ print ('\n add_to_dict(my_english_dict, "kimchi", "My fav. food"):' )
89
+ add_to_dict (my_english_dict , "kimchi" , "My fav. food" )
90
+
91
+
92
+ print ("\n \n ###### get_from_dict ######\n " )
93
+
94
+ # Should not work. First argument should be a dict.
95
+ print ('\n get_from_dict("hello", "kimchi"):' )
96
+ get_from_dict ("hello" , "kimchi" )
97
+
98
+ # Should not work. Word to search from is required.
99
+ print ('\n get_from_dict(my_english_dict):' )
100
+ get_from_dict (my_english_dict )
101
+
102
+ # Should not work. Word is not found.
103
+ print ('\n get_from_dict(my_english_dict, "galbi"):' )
104
+ get_from_dict (my_english_dict , "galbi" )
105
+
106
+ # Should work and print the definiton of 'kimchi'
107
+ print ('\n get_from_dict(my_english_dict, "kimchi"):' )
108
+ get_from_dict (my_english_dict , "kimchi" )
109
+
110
+ print ("\n \n ###### update_word ######\n " )
111
+
112
+ # Should not work. First argument should be a dict.
113
+ print ('\n update_word("hello", "kimchi"):' )
114
+ update_word ("hello" , "kimchi" )
115
+
116
+ # Should not work. Word and definiton are required.
117
+ print ('\n update_word(my_english_dict, "kimchi"):' )
118
+ update_word (my_english_dict , "kimchi" )
119
+
120
+ # Should not work. Word not found.
121
+ print ('\n update_word(my_english_dict, "galbi", "Love it."):' )
122
+ update_word (my_english_dict , "galbi" , "Love it." )
123
+
124
+ # Should work.
125
+ print ('\n update_word(my_english_dict, "kimchi", "Food from the gods."):' )
126
+ update_word (my_english_dict , "kimchi" , "Food from the gods." )
127
+
128
+ # Check the new value.
129
+ print ('\n get_from_dict(my_english_dict, "kimchi"):' )
130
+ get_from_dict (my_english_dict , "kimchi" )
131
+
132
+
133
+ print ("\n \n ###### delete_from_dict ######\n " )
134
+
135
+ # Should not work. First argument should be a dict.
136
+ print ('\n delete_from_dict("hello", "kimchi"):' )
137
+ delete_from_dict ("hello" , "kimchi" )
138
+
139
+ # Should not work. Word to delete is required.
140
+ print ('\n delete_from_dict(my_english_dict):' )
141
+ delete_from_dict (my_english_dict )
142
+
143
+ # Should not work. Word not found.
144
+ print ('\n delete_from_dict(my_english_dict, "galbi"):' )
145
+ delete_from_dict (my_english_dict , "galbi" )
146
+
147
+ # Should work.
148
+ print ('\n delete_from_dict(my_english_dict, "kimchi"):' )
149
+ delete_from_dict (my_english_dict , "kimchi" )
150
+
151
+ # Check that it does not exist
152
+ print ('\n get_from_dict(my_english_dict, "kimchi"):' )
153
+ get_from_dict (my_english_dict , "kimchi" )
154
+
155
+ # \/\/\/\/\/\/\ END DO NOT TOUCH \/\/\/\/\/\/\
0 commit comments