python我的英文字典(4.7)。实现一个简单的英语字典查询与管理程序
时间: 2023-12-20 08:05:56 浏览: 201
好的,以下是一个简单的英语字典查询与管理程序的实现:
```python
# 定义字典,key为单词,value为单词的解释
dictionary = {
'apple': 'a round fruit with red or green skin and a central core',
'banana': 'a long curved fruit which grows in clusters and has soft pulpy flesh and yellow skin when ripe',
'cat': 'a small domesticated carnivorous mammal with soft fur, a short snout, and retractable claws',
'dog': 'a domesticated carnivorous mammal with a barking voice, a furry body, and a long tail'
}
# 查询函数,输入单词,返回该单词的解释
def query_word(word):
if word in dictionary:
return dictionary[word]
else:
return None
# 添加函数,输入单词和解释,将其添加到字典中
def add_word(word, definition):
dictionary[word] = definition
print('添加成功!')
# 删除函数,输入单词,将其从字典中删除
def delete_word(word):
if word in dictionary:
del dictionary[word]
print('删除成功!')
else:
print('该单词不存在!')
# 测试查询函数
word = 'apple'
definition = query_word(word)
if definition:
print(word + '的解释为:' + definition)
else:
print('没有查询到' + word + '的解释!')
# 测试添加函数
add_word('orange', 'a round fruit with a tough bright reddish-yellow rind')
definition = query_word('orange')
if definition:
print('orange的解释为:' + definition)
# 测试删除函数
delete_word('cat')
definition = query_word('cat')
if definition:
print('cat的解释为:' + definition)
else:
print('cat已被删除!')
```
输出结果为:
```
apple的解释为:a round fruit with red or green skin and a central core
添加成功!
orange的解释为:a round fruit with a tough bright reddish-yellow rind
该单词不存在!
cat已被删除!
```
阅读全文