-1

文字とインデックスを取り、クロスワードの答えを吐き出すプログラムを書いています (クロスワード ソルバーではなく、クロスワードの解決を支援するツールです)。

私は 2 つのバージョンのアルゴリズムを作成しましたが、どちらも正しく動作していないようです。私が最初に試したのはこれでした:

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

ちょっと醜いことはわかっていますが、これはアルゴリズムの概念実証にすぎません。ユーザー入力は後で変更されます。とにかく、これは何を試しても空のリストを返すようです。

2 番目のバージョンは基本的に同じですが、ブール演算子の代わりにネストされた 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 に答える