制作英文学习词典。编写程序制作英文学习词典,词典有 3 个基本功能:添加、
查询和退出。程序读取源文件路径下的 txt 格式词典文件,若没有就创建一个。
词典文件存储方式为“英文单词 中文单词”,每行仅有一对中英释义。程序会根据
用户的选择进入相应的功能模块,并显示相应的操作提示。当添加的单词已存在
时,显示“该单词已添加到字典库”;当查询的单词不存在时,显示“字典库中未找
到这个单词”。用户输入其他选项时,提示“输入有误”。
Python有关制作英文学习词典问题。
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
4条回答 默认 最新
- 神仙别闹 2021-12-31 22:21关注
可以参考下这个
#英文字典 def oppendict(): dir = 'mydict.txt' dicts = {} tf=open(dir,'r+') print('本字典已有的内容:') for item in tf: k,v=tuple(item.replace('\n','').split(':')) print(k,v) dicts[k]=v tf.close() return dicts dicts=oppendict() #查字典 def lookup(): quest=input("请输入要查询的单词") result=dicts.get(quest,"字典库中未找到这个单词") print('该单词的解释为:'+result+'\n') #插入新单词 def inserts(): word = input("请输入要插入的单词:") if dicts.get(word): print('该单词已添加到字典库'+'\n') else: explain=input("请输入该单词的解释:") dicts[word]=explain #更新单词解释 def renew(): word = input('请输入需要更新解释的单词:') explain=input("请输入单词的新解释:") dicts[word]=explain #删除 def deldict(): word = input('请输入删除的单词:') if dicts.get(word): del dicts[word] else: print('没有这个单词') print(dicts) #存档 def savedict(): dir='mydict.txt' tf = open(dir,'w+') for item in dicts.items(): tf.write(':'.join(item) + '\n' ) tf.close() def meun(): meuns=['1.查询','2.新增','3.更新','4.删除','5.退出'] funcname = ['','lookup','inserts' ,'renew' , 'deldict'] while True: print('\n'.join(meuns)) trs = eval(input('请用数字选择:')) if trs==5: break elif trs in range(1,5): eval(funcname[trs])() else: print('输入有误'+'\n') if __name__=='__main__': print('欢迎使用本字典'.center(20,'=')) meun() print('再见'.center(20,'=')) savedict() dicts = oppendict()
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 3无用
悬赏问题
- ¥15 PADS Logic 原理图
- ¥15 PADS Logic 图标
- ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
- ¥20 气象站点数据求取中~
- ¥15 如何获取APP内弹出的网址链接
- ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
- ¥50 STM32单片机传感器读取错误
- ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
- ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
- ¥15 机器人轨迹规划相关问题