Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.3,查找字谜?_Python - Fatal编程技术网

Python 3.3,查找字谜?

Python 3.3,查找字谜?,python,Python,我正在尝试用Python编写一个程序来查找字谜。这是我目前的代码: def anagram(word,checkword): for letter in word: if letter in checkword: checkword = checkword.replace(letter, '') else: return False return True while True

我正在尝试用Python编写一个程序来查找字谜。这是我目前的代码:

def anagram(word,checkword):
    for letter in word:  
        if letter in checkword:  
            checkword = checkword.replace(letter, '') 
        else:  
            return False  
    return True  

while True:
    f = open('listofwords.txt', 'r')
    try:
        inputted_word = input('Word? ')
        for word in f:
            word = word.strip()
            if len(word)==len(inputted_word):
                if word == inputted_word:
                    continue
                elif anagram(word, inputted_word):
                    print(word)
                        #try:
                            #if word == 1:
                            #print ('The only anagram for', user_input, 'is', word)
                        #elif word > 1:
                            #print ('The anagrams for', user_input, 'are', word)
                        #except TypeError:
                            #pass
    except:
        break 
我在输出字谜时遇到问题。字谜应该在一行中,并且措辞应该反映发现的字谜的数量。比如

“只有一个(插入字谜)(插入输入的单词)”

“有(插入字谜)用于(插入输入的单词)”

“没有(插入输入的单词)的字谜”

“插入的单词不在词典中”)

以下是一些提示:

首先,如果在打印任何一个字谜之前必须打印字谜的数量,那么在循环时需要保留一个字谜列表。大概是这样的:

anagrams = []
for word in f:
    word = word.strip()
    if len(word)==len(inputted_word):
        if word == inputted_word:
            continue
        elif anagram(word, inputted_word):
            anagrams.append(word)
现在,您只需根据
字谜
列表中的内容,找出如何在末尾打印正确的文本

至于你尝试了什么:

#try:
    #if word == 1:
    #print ('The only anagram for', user_input, 'is', word)
#elif word > 1:
    #print ('The anagrams for', user_input, 'are', word)
#except TypeError:
    #pass
这不可能奏效。首先,
word
是一个词,所以它不可能等于
1
,也不可能大于
1
。还有,如果你只查过,比如说,字典里的前20个单词,找到了第一个字谜,你怎么知道这是唯一的字谜?这本词典的其余部分可能有1000本。在你读完整本词典之前,你无法决定打印哪个句子


同时,请注意,“字典中只有一个”和“不在字典中”有不同的情况。因此,您需要某种“在字典中找到输入的单词”的标志,您可以在
if
语句中设置该标志。或者,你可以省略这个特殊的情况,例如,在最后,如果你有0个结果,你知道它不在字典里。这取决于您是希望在最后还是在循环内部有更多的逻辑。

我们不是来为您做功课的。你试过什么?好吧,我编辑了这个问题。代码中的注释就是我所尝试的。对于属于另一个单词的任何单词,您的anagram()函数都将返回true,例如,如果您将“cat”与“thatcher”进行对比,您仍然会得到true。我建议你把这两个单词都列成单子,然后比较排序后的单子。你确定你贴的句子真的是你老师想要的吗?“只有一个‘上帝’代表‘狗’”、“有‘手臂’、‘公羊’代表‘马’”、“字典里没有‘foobar’”都不是很好的句子……你呢?你真的不应该因为不喜欢答案就转发。