在python中搜索文本文件以查找输入的单词

2024-05-23 20:54:32 发布

您现在位置:Python中文网/ 问答频道 /正文

所以基本上我在一个文本文件中有一个很大的单词列表,当用户输入一个来检查拼写时,我希望能够搜索到匹配的单词,这就是我目前所拥有的。

f = open('words.txt', 'r')
wordCheck = input("please enter the word you would like to check the spelling of: ")

for line in f:
    if 'wordCheck' == line:
        print ('That is the correct spelling for '+wordCheck)
    else:
        print ( wordCheck+ " is not in our dictionary")
    break

当我输入一个单词时,我会立刻得到else语句,我甚至不认为它会读过文本文件。 我应该改用while循环吗?

while wordCheck != line in f

我是python新手,最终我希望用户能够输入一个单词,如果拼写不正确,程序应该打印出一个匹配单词的列表(75%的字母或更多匹配)。

任何帮助都将不胜感激


Tags: the用户in列表forislineopen