python制作英文學習字典案列


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("請輸入英文單詞:")
        
        if key not in keys:
            value = input("請輸入中文單詞:")
            dic[key] = value
            keys.append(key)
            print("單詞已經添加成功")
        else:
            print("該單詞已經添加至字典庫")
    elif n =="查詢":
        key = input("請輸入英文單詞:")
        if key in keys:
            print("中文意思為:"+dic[key])
        else:
            print("字典中未找到這個單詞")
    elif n =="退出":        
        return 1
    else:        
        print("輸入有誤")
        return 0
    
def wdic():#寫入文件代碼 通過keys的順序寫入
    with open('dic.txt','w') as fw:
        for k in keys:
            fw.write(k+':'+dic[k]+'\n') 
    
 
if __name__=="__main__":
    keys = [] #用來存儲讀取的順序
    dic = {}
    while True:
        rdic()
        n = centre()
        wdic()
        if n == 0:
            continue
        elif n == 1:
            break            
        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM