#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Date : 2021/5/25
# @Name : ZhouZongXin
"""
读取dict.txt
使用input输入一个单词,打印出这个单词的解释,如果没有这个单词则打印"没有找到该单词"
提示:
每个单词占一行
单词与解释直接有空格
单词按照从小到大排列
"""
import os
PAth = os.path.dirname(__file__)
file = os.path.join(PAth, "dict.txt")
query = str(input("请输出查询的单词:"))
f = open(file, "r")
for line in f:
r = line.split()
if str(r[0]) > query:
print("没有找到该单词")
break
elif str(r[0]) == query:
print("对应单词的解释为:", line[len(r[0]):].lstrip())
break
else:
print("没有找到该单词")
f.close()
dict.txt 文件如下: