2000字范文,分享全网优秀范文,学习好帮手!
2000字范文 > 9.5.4英语词典。设计字典记录小张新学的英文单词和中文翻译 并能根据英文来查找中文

9.5.4英语词典。设计字典记录小张新学的英文单词和中文翻译 并能根据英文来查找中文

时间:2022-06-23 17:07:03

相关推荐

9.5.4英语词典。设计字典记录小张新学的英文单词和中文翻译 并能根据英文来查找中文

首先我们先定义两个函数

1添加新学单词函数:add_dic我们认为如果字典已经存在则不再添加,所以我们使用setdefault函数进行,因为不需要返回值,所以不用return

def add_dic(a,b):englishlist.setdefault(a,b)

2寻找函数

因为要输出查找值,所以先输出查找值,再用字典查找即可

def search_dic(c):print(c,end=" ")print(englishlist[c])

然后是主程序

注意事项

1因为在x=3的时候会退出,所以我们使用while循环

2在每个循环后要重新输入x

x=eval(input("choose 1-input,2-look for,3-exit"))englishlist={}while x!=3:if x==1:a=input("Please input an English word:")b=input("Please input the Chinese meaning")add_dic(a,b)x=eval(input("choose 1-input,2-look for,3-exit"))elif x==2:c=input("Please input the word you want ro look for")search_dic(c)x=eval(input("choose 1-input,2-look for,3-exit"))elif x==3:break

完整代码如下:

def add_dic(a,b):englishlist.setdefault(a,b)def search_dic(c):print(c,end=" ")print(englishlist[c])x=eval(input("choose 1-input,2-look for,3-exit"))englishlist={}while x!=3:if x==1:a=input("Please input an English word:")b=input("Please input the Chinese meaning")add_dic(a,b)x=eval(input("choose 1-input,2-look for,3-exit"))elif x==2:c=input("Please input the word you want ro look for")search_dic(c)x=eval(input("choose 1-input,2-look for,3-exit"))elif x==3:break

​​​​​​​

9.5.4英语词典。设计字典记录小张新学的英文单词和中文翻译 并能根据英文来查找中文翻译 当用户输入1 按提示添加新的单词和中文;用户输入2可查找英文单词的对应中文翻译 输入3 退出程序。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。