本文實例為大家分享了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()
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
