-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
69 lines (58 loc) · 2.46 KB
/
main.py
File metadata and controls
69 lines (58 loc) · 2.46 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
import requests
def fetch_word_data(word):
"""Fetch word data from the DictionaryAPI."""
BASE_URL = "https://api.dictionaryapi.dev/api/v2/entries/en/"
try:
response = requests.get(BASE_URL + word)
response.raise_for_status() # Will raise an error for bad responses (non-200 status)
return response.json()
except requests.exceptions.HTTPError as http_err:
print(f"HTTP error occurred: {http_err}")
except requests.exceptions.ConnectionError:
print("Connection error. Please check your internet connection.")
except Exception as err:
print(f"An error occurred: {err}")
return None
def display_meanings(word: str, data):
"""Display meaning(s) of the word."""
print(f"\n<>-<>-<>---( {word.title()} )---<>-<>-<>\n")
phonetics = data[0].get("phonetics", [])
if phonetics:
text = phonetics[0].get("text")
if text:
print(f"\nPhonetic: {text}")
meanings = data[0].get("meanings", [])
if not meanings:
print("\nNo meanings found.")
return
for meaning in meanings:
part_of_speech = meaning["partOfSpeech"]
print(f"\n------( {part_of_speech} )------")
for i, d in enumerate(meaning["definitions"], start=1):
print(f"\n------( Definition {i} )------\n\n{d['definition']}")
if d.get("example"):
print("\n------( Example )------\n\n")
print(f"{d['example']}")
if d.get("synonyms"):
print("\n------( Synonyms )------\n\n")
print(", ".join(d["synonyms"]))
if d.get("antonyms"):
print("\n------( Antonyms )------\n\n")
print(", ".join(d["antonyms"]))
def main():
while True:
print("\n<--<--<---( Dictionary )--->-->-->\n")
word = input("\nEnter the word: ").lower()
# Fetch data for the copied or entered word
data = fetch_word_data(word)
if not data:
print(f"\n{word} not found! Please enter another word.\n")
continue # Retry on error or invalid word
display_meanings(word, data)
# Option to quit
choice = input("\nAnother word? (Y/N): ").strip().lower()
if choice in ["n", "no", "exit", "q"]:
print("\nHave a nice day!\n")
break
if __name__ == "__main__":
main()