python如何制作英文字典

yipeiwu_com5年前Python基础

本文实例为大家分享了python制作英文字典的具体代码,供大家参考,具体内容如下

功能有添加单词,多次添加单词的意思,查询,退出,建立单词文件。

keys=[]
dic={}
def rdic():
  fr = open('dic.txt','r')  
  for line in fr:
    line = line.replace("\n",'')
    v = line.split(':')
    dic[v[0]] = v[1]
    keys.append(v[0])
  fr.close()
def centre():
  n = input("请输入进入相应模块(添加、查询、退出):")
  if n == "添加":
    key= input("plsease input English:")
    if key not in keys:
      value=input("please input Chinese:")
      dic[key]=value
      keys.append(key)
    else :
      t=input("如果添加新的意思请输入 Y,否则输入N:")
      if ( t=='Y'):
        temp=""
        temp=temp+dic[key]
        key1= input("请输入中文")
        temp=temp+","+key1
        print(temp)
        #keys.append(temp)
        dic[key]=temp
        print(dic)
        return 0
      else:
        return 0
  elif n== "查询":
    key= input("plsease input English:")
    print(keys)
    print(dic)
    if key not in keys:
      print("the english not in the dic.")
    else :
      print(dic[key])
  elif n == "退出" :
    return 1
  else :
    print("输入有误")
    return 0
def wdic():
  #print("!")
  with open('dic.txt','w') as fw:
    for k in keys:
      fw.write(k+':'+dic[k]+'\n')
def main():
  rdic()
  while True:
    print(keys)
    print(dic)
    n=centre()
    print(keys)
    print(dic)
    if n==1:
      break
    if n==0:
      continue
  wdic()
main()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

教你使用python实现微信每天给女朋友说晚安

教你使用python实现微信每天给女朋友说晚安

本文为大家分享了教你用微信每天给女朋友说晚安的python实战,供大家参考,具体内容如下 但凡一件事,稍微有些重复。我就考虑怎么样用程序来实现它。 这里给各位程序员朋友分享如何每天给朋友...

Linux(Redhat)安装python3.6虚拟环境(推荐)

python是3.6 centos 6 64位 1.安装python 2.安装pip wget https://bootstrap.pypa.io/get-pip.py --no-c...

python实现最长公共子序列

python实现最长公共子序列

最长公共子序列python实现,最长公共子序列是动态规划基本题目,下面按照动态规划基本步骤解出来。 1.找出最优解的性质,并刻划其结构特征 序列a共有m个元素,序列b共有n个元素,如果a...

Python在Console下显示文本进度条的方法

进度条实现原理 进度条和一般的print区别在哪里呢? 答案就是print会输出一个\n,也就是换行符,这样光标移动到了下一行行首,接着输出,之前已经通过stdout输出的东西依旧保留,...

解决python2 绘图title,xlabel,ylabel出现中文乱码的问题

解决python2 绘图title,xlabel,ylabel出现中文乱码的问题

绘制图形时使用了中文标题,会出现乱码 原因是matplotlib.pyplot在显示时无法找到合适的字体。 先把需要的字体(在系统盘C盘的windows下的fonts目录内)添加到Fo...