-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.4 Geek Translator - Dictionaries.py
82 lines (67 loc) · 2.63 KB
/
5.4 Geek Translator - Dictionaries.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
# Geek translator - Dictionaries
# Demonstrates using dictionaries
geek = {"404": "clueless. From the web error message 404, meaning page not found.", "Googling":
"searching the Internet for background information on a person.","Keyboard Plaque":
"the collection of debris found in computer keyboards.","Link Rot":"the process by which web page links become obsolete.",
"Percussive Maintenance":"the act of striking an electronic device to make it work.",
"Uninstalled": "being fired. Especially popular during the dot-bomb era."}
choice = None
while choice != "0":
print(
"""
Geek Translator
0 - Exit
1 - Look up a Geek Term
2 - Add a Geek Term
3 - Redefine a Geek Term
4 - Delete a Geek Term
5 - Show all terms
"""
)
choice = input("Choice:" )
print()
# exit
if choice == "0":
print("Good-bye.")
# get a definition
elif choice == "1":
term = input("What term do you want me to translate?: ")
if term in geek:
definition = geek[term]
print("\n", term, "means", definition)
else:
print("\nSorry, I don't know", term)
# add a key-value pair
elif choice == "2":
term = input("What term do you want me to add?: ")
if term not in geek:
definition = input("\nWhat's the definition?: ")
geek[term] = definition
print("\n", term, "has been added.")
else:
print("\nThat term already exists! Try redefining it.")
# replace a key-value pair
elif choice == "3":
term = input("What term do you want me to redefine?")
if term in geek:
definition = input("What's the new definition?")
geek[term] = definition
print("\n", term, "has been redefined.")
else:
print("\nThat term doesn't exist! Try adding it.")
# delete a key-value pair
elif choice == "4":
term = input("What term do you want me to delete?: ")
if term in geek: # Check it exists otherwise you'll get an error
del geek[term]
print("\nOkay, I deleted", term)
else:
print("\nI can't do that!", term, "Doesn't exist in the dictionary.")
# show all terms
elif choice == "5":
for key in geek.keys():
print(key)
# some unknown choice
else:
print("\nSorry, but", choice, "isn't a valid choice.")
input ("\n\nPress the enter key to exit.")