【Python】英文单词字典。编写制作英文学习词典,词典有3个基本功能:添加(write)、查询(readline)和退出。

f1=open('dictionary.txt','a+')
print('1——————————添加')
print('2——————————查询')
print('3——————————退出')
f1.close()
while 1:
    a = eval(input('请问您想要执行什么?'))
    if a==1:
        print('请添加英文单词 中文单词(若有多个意思用逗号隔开)的格式:')
        word1=input()
        read1=open('dictionary.txt','a+',encoding='utf-8')
        read1.write(word1 + '\n')
        j = 0
        for line1 in read1.readlines():
            Line1 = line1.replace('\n', '')
            Line1 = line1.split(" ")
            if word1 == Line1[0]:
                j = 1
                break
        if j == 1:
            print('该单词已添加到单词库')
        else:
            read1.write(word1+'\n')
        read1.close()
    elif a==2:
        print("请输入您想要查询的单词:")
        word2=input()
        read2 = open('dictionary.txt', 'r',encoding='UTF-8')
        read2.seek(0)
        i = 0
        for line2 in read2.readlines():
            Line2 = line2.replace('\n', '')
            Line2=line2.split(" ")
            if word2==Line2[0]:
                i=1
                break
        if i==1:
            print(f'该单词为:{Line2[0]} {Line2[1]}')
        elif i==0:
            print('字典库中未找到该单词')
        read2.close()
    elif a==3:
        print('即将退出字典')
        break
    else:
        print('输入有误!请重新输入!')
        continue

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值