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设计】。

相关文章

Django项目使用ckeditor详解(不使用admin)

Django项目使用ckeditor详解(不使用admin)

效果图: 1.安装django-ckeditor pip install django-ckeditor 如果需要上传图片或者文件,还需要安装pillow pip insta...

python redis 删除key脚本的实例

单机模式 代码片段 安装 pip install redis import redis r = redis.Redis(host='192.168.1.3', port=6188,d...

python中获得当前目录和上级目录的实现方法

获取当前文件的路径: from os import path d = path.dirname(__file__) #返回当前文件所在的目录 # __file__ 为当前文件...

python 实现求解字符串集的最长公共前缀方法

问题比较简单,给定一个字符串集合求解其中最长的公共前缀即可,这样的问题有点类似于最长公共子序列的问题,但是比求解最长最长公共子序列简单很多,因为是公共前缀,这样的话只需要挨个遍历即可,只...

对Python 简单串口收发GUI界面的实例详解

忙活了三个多小时,连学带做,总算是搞出来了一个具有基本功能的串口通信PC机的GUI界面,Tkinter在python中确实很好用,而且代码量确实也很少,不足的是Tkinter不自带com...