forked from kal179/Beginners-Python-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary.py
85 lines (67 loc) · 1.94 KB
/
dictionary.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
global dictionary
dictionary = {}
class Dict:
def __init__(self, word, meaning):
self.word = word
self.meaning = meaning
def add_new(self):
dictionary[self.word] = self.meaning
print "Word Successfully Added"
def delete_word(self):
try:
del dictionary[self.word]
print "Word Successfully Deleted"
except KeyError:
print "The Word Does Not Exist in Dictionary. Try Again!"
def edit_word(self):
try:
dictionary[self.word] = self.meaning
print "Word Was Successfully Edited"
except KeyError:
print "The Word You Trying To Edit Does Not Exist in Dictionary!"
def view_word(self):
try:
print dictionary[self.word]
except KeyError:
print "The Word is not in Dictionary."
def view_all(self):
for i in dictionary.keys():
print(i + " : " + dictionary[i])
def start():
get_op = raw_input("Add, Delete, Edit, View, View all : ")
if get_op in ["add", "Add"]:
get_word = raw_input("Word to add : ")
get_meaning = raw_input("Meaning : ")
new = Dict(get_word, get_meaning)
new.add_new()
elif get_op in ["delete", "Delete"]:
get_word_to_del = raw_input("Word to delete : ")
delete = Dict(get_word_to_del, None)
delete.delete_word()
elif get_op in ["edit", "Edit"]:
get_word_to_edit = raw_input("Word to edit : ")
get_new_meaning = raw_input("New meaning : ")
mean = Dict(get_word_to_edit, get_new_meaning)
mean.edit_word()
elif get_op in ["view", "View"]:
get_word_to_view = raw_input("Word to view : ")
view = Dict(get_word_to_view, None)
view.view_word()
elif get_op in ["view all", "View All", "View all"]:
nothing = Dict(None, None)
nothing.view_all()
else:
print "Invalid Input. Try again!"
def end():
quit()
def main():
while True:
s_or_e = raw_input("Start or End : ")
if s_or_e == "Start":
start()
print(" ")
continue
else:
end()
if __name__ == "__main__":
main()