-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.py
More file actions
39 lines (33 loc) 路 1.12 KB
/
Copy pathscript.py
File metadata and controls
39 lines (33 loc) 路 1.12 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
"""
Spyder Editor
In this script the user enter a word via command prompt
and gets its definition as an output.
"""
import json
from difflib import get_close_matches
data = json.load(open("data.json"))
def translate(w):
w = w.lower()
if w in data:
return data[w]
elif w.title() in data: #if user entered "texas" this will check for "Texas" as well.
return data[w.title()]
elif w.upper() in data: #in case user enters words like USA or NATO
return data[w.upper()]
elif len(get_close_matches(w, data.keys())) > 0:
yn = input("Did you mean %s instead? Enter Y if yes, or N if no: " % get_close_matches(w, data.keys())[0])
if yn == "Y":
return data[get_close_matches(w, data.keys())[0]]
elif yn == "N":
return "The word doesn't exist. Please double check it."
else:
return "We didn't understand your entry."
else:
return "The word doesn't exist. Please double check it."
word = input("Enter word: ")
output = translate(word)
if type(output) == list:
for item in output:
print(item)
else:
print(output)