用python写一个英语小词典
时间: 2023-10-10 13:06:32 浏览: 50
好的,这里是一个简单的 Python 英语小词典,它可以根据用户输入的单词查询该单词的解释:
```python
import json
# 读取词典文件
with open('dictionary.json', 'r') as f:
dictionary = json.load(f)
# 查询单词
while True:
word = input('请输入要查询的单词:')
if word in dictionary:
print(dictionary[word])
else:
print('抱歉,词典中未收录该单词。')
```
需要注意的是,这个小词典需要一个名为 `dictionary.json` 的文件,其中包含了单词的解释。你可以自己手动创建这个文件,也可以从网上下载一些现成的单词数据集。如果你想要自己创建文件,可以按照以下格式:
```json
{
"apple": "a round fruit with red or green skin and a white inside",
"banana": "a long curved fruit with a yellow skin and soft sweet flesh",
"cat": "a small domesticated carnivorous mammal with soft fur, a short snout, and retractable claws"
}
```
其中,每个单词都是一个键,对应的值是该单词的解释。你可以根据自己的需求添加或删除单词,也可以修改现有单词的解释。