【发布时间】:2018-04-07 06:16:47
【问题描述】:
我有一个名为“dictionary.txt”的文件,其中包含按字母顺序排列的字典中的单词。
我想搜索某些单词是否在这本词典中。 谁能帮我编写可以打开这个文件的代码,然后我写一个单词作为输入,然后我收到一个输出,说明该单词是否在字典文件中?
这是我目前所拥有的:-
dicfile = open('dictionary.txt', 'r')
word = input('enter a word: ')
if word in dicfile:
print('true')
else:
print('false')
【问题讨论】:
-
很好的解释你能展示你的代码吗?
-
dicfile = open('dictionary.txt', 'r') word = input('enter a word: ') if word in dicfile: print('true') else: print('false ')
-
我仍然是 python 的初学者,所以非常感谢任何帮助。
标签: python python-3.x file