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
【Python】英文单词字典。编写制作英文学习词典,词典有3个基本功能:添加(write)、查询(readline)和退出。
最新推荐文章于 2024-05-20 17:19:54 发布