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

 

  • 6
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值