【发布时间】:2018-04-09 12:46:33
【问题描述】:
此函数将从 .txt 文件的列表中搜索字谜,我希望能够检查字谜并返回我输入的单词的所有字谜,如果它不是字谜,它将返回输入,当我在下面的代码中执行此操作时,它会遍历 for 循环,然后忽略我的第一个 if 语句并直接转到我的 else 语句。我该如何解决这个问题?
def find_in_dict():
input_word = input("Enter input string)")
sorted_word = ''.join(sorted(input_word.strip()))
a_word = ''.join((input_word.strip()))
word_file = open("filename", "r")
word_list = {}
for text in word_file:
simple_text = ''.join(sorted(text.strip()))
word_list.update({text.strip(): simple_text})
alist = []
for key, val in word_list.items():
if val == sorted_word:
alist.append(key)
return alist
else:
return "No words can be formed from:" + a_word
【问题讨论】:
-
使 if 语句中的条件评估为真 - python 不仅仅是“跳过”代码。
标签: python