溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python如何制作英文字典

發(fā)布時間:2020-09-18 09:23:03 來源:腳本之家 閱讀:305 作者:黑脈金 欄目:開發(fā)技術

本文實例為大家分享了python制作英文字典的具體代碼,供大家參考,具體內(nèi)容如下

功能有添加單詞,多次添加單詞的意思,查詢,退出,建立單詞文件。

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()

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI