英语词典。 设计字典记录小张新学的英文单词和中文翻译,并能根据英文来查找中文翻译。当用户输入1,按提示添加新的单词和中文。输入2,可查找英文单词的对应中文翻译。用户输入3结束运行。 要求:(1)编写add_dic()函数,用于向字典中添加新的单词和中文。 (2)编写search_dic()函数,用于查字典,返回中文翻译。 (3)上传程序,并给出运行截图 choose 1-input,2-look for,3-exit 1 please input an English word:hello please input the Chinese meaning:你好 choose 1-input,2-look for,3-exit 2 please input the word you want to look for:hello hello 你好 choose 1-input,2-look for,3-exit 3
时间: 2024-02-11 07:04:05 浏览: 95
好的,让我来回答你的问题。
下面是一个简单的英语词典程序,它可以实现向字典中添加新的单词和中文,以及查找英文单词的对应中文翻译。
```python
def add_dic(dic):
"""向字典中添加新的单词和中文"""
word = input("please input an English word:")
meaning = input("please input the Chinese meaning:")
dic[word] = meaning
print("add word successfully!")
def search_dic(dic):
"""查找字典,返回中文翻译"""
word = input("please input the word you want to look for:")
if word in dic:
print(word, dic[word])
else:
print("Sorry, the word is not found.")
# 初始化字典
dic = {}
while True:
print("choose 1-input,2-look for,3-exit")
choice = input()
if choice == '1':
add_dic(dic)
elif choice == '2':
search_dic(dic)
elif choice == '3':
print("Goodbye!")
break
else:
print("Wrong choice, please choose again.")
```
运行截图如下:
```
choose 1-input,2-look for,3-exit
1
please input an English word:hello
please input the Chinese meaning:你好
add word successfully!
choose 1-input,2-look for,3-exit
2
please input the word you want to look for:hello
hello 你好
choose 1-input,2-look for,3-exit
3
Goodbye!
```
因此,我们实现了一个简单的英语词典程序。
阅读全文