-1

我正在编写一个程序,它接受字母和索引并吐出填字游戏答案(不是填字游戏求解器,而是帮助解决填字游戏的工具,如果有意义的话)。

我已经编写了两个版本的算法,但似乎都不能正常工作。我尝试的第一个是这样的:

fin = open('words.txt')

def answer_finder():
    global fin
    possible_answers = []
    length = int(raw_input("How long is the word?"))
    letter_1 = raw_input("What is the first letter that you know?").lower
    letter_1_index = int(raw_input("How many letters into the word is that letter?")) - 1
    letter_2 = raw_input("What is the second letter that you know?").lower
    letter_2_index = int(raw_input("How many letters into the word is that letter?")) - 1
    letter_3 = raw_input("What is the third letter that you know?").lower
    letter_3_index = int(raw_input("How many letters into the word is that letter?")) - 1
    for i in fin:
        if len(i) == length:
            if i[letter_1_index] == letter_1 and i[letter_2_index] == letter_2 and i[letter_3_index] == letter_3:
                possible_answers.append(i)
    return possible_answers

我意识到这有点难看,但这更像是算法的概念证明。稍后将更改用户输入。无论如何,无论我尝试什么,这似乎都会返回一个空列表。

第二个版本基本相同,但依赖于嵌套的 if 语句而不是布尔运算符:

def answer_finder():
    global fin
    possible_answers = []
    length = int(raw_input("How long is the word?"))
    letter_1 = raw_input("What is the first letter that you know?").lower
    letter_1_index = int(raw_input("How many letters into the word is that letter?")) - 1
    letter_2 = raw_input("What is the second letter that you know?").lower
    letter_2_index = int(raw_input("How many letters into the word is that letter?")) - 1
    letter_3 = raw_input("What is the third letter that you know?").lower
    letter_3_index = int(raw_input("How many letters into the word is that letter?")) - 1
    for i in fin:
        if len(i) == length:
            if i[letter_1_index] == letter_1:
                if i[letter_2_index] == letter_2:
                    if i[letter_3_index] == letter_3:
                        possible_answers.append(i)
    return possible_answers

这也返回一个空列表。我使用的单词列表来自这里。我假设我遗漏了一些明显的东西,因为我不熟悉使用外部文件。我应该指出,这些函数是前者的原型,并且都可以正常工作:

def greater_than_20():
    global fin
    li = []
    for i in fin:
        if len(i) > 20:
            li.append(i)
    return li

def first_letter_length_finder():
    global fin
    length = int(raw_input("How long is the word?"))
    first_letter = raw_input("What is the first letter?")
    li = []
    for i in fin:
        if len(i) == length and i[0] == first_letter:
            li.append(i)
    print li
    return li

编辑:仅供参考,这是完整的代码(包括注释掉的代码部分。)

fin = open('words.txt')
print fin

def greater_than_20():
    global fin
    li = []
    for i in fin:
        if len(i) > 20:
            li.append(i)
    return li

def first_letter_length_finder():
    global fin
    length = int(raw_input("How long is the word?"))
    first_letter = raw_input("What is the first letter?")
    li = []
    for i in fin:
        if len(i) == length and i[0] == first_letter:
            li.append(i)
    print li
    return li

def answer_finder():
    global fin
    possible_answers = []
    length = int(raw_input("How long is the word?"))
    letter_1 = raw_input("What is the first letter that you know?").lower
    letter_1_index = int(raw_input("How many letters into the word is that letter?")) - 1
    letter_2 = raw_input("What is the second letter that you know?").lower
    letter_2_index = int(raw_input("How many letters into the word is that letter?")) - 1
    letter_3 = raw_input("What is the third letter that you know?").lower
    letter_3_index = int(raw_input("How many letters into the word is that letter?")) - 1
    for i in fin:
        if len(i) == length:
#            if i[letter_1_index] == letter_1 and i[letter_2_index] == letter_2 and i[letter_3_index] == letter_3:
#                possible_answers.append(i)
            if i[letter_1_index] == letter_1:
                if i[letter_2_index] == letter_2:
                    if i[letter_3_index] == letter_3:
                        possible_answers.append(i)
    return possible_answers
4

2 回答 2

1

您正在使用该string.lower方法,但您错过了().

>>> letter_3 = raw_input("What is the third letter that you know?").lower
What is the third letter that you know?a
>>> letter_3
<built-in method lower of str object at 0x104e514e0>

>>> letter_3 = raw_input("What is the third letter that you know?").lower()
What is the third letter that you know?a
>>> letter_3
'a'

没有括号,您分配的不是函数的.lower而是函数本身。

编辑:作为记录(因为我不能留下评论),你的使用for i in fin方法很好,因为文件是可迭代的,由\n.

于 2013-09-02T23:45:24.533 回答
0

在尝试使用数据之前,您需要先从文件中读取数据。

fin = file('words.txt').readlines ()
于 2013-09-02T23:29:03.987 回答