I am writing a program that takes letters and indexes and spits out crossword answers (not a crossword solver, but a tool to aid in solving crosswords, if that makes sense).
I've written two versions of the algorithm, but neither seems to work correctly. The first one I tried was this:
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
I realize it's kinda ugly, but this was more of a proof of concept for the algorithm. The user input will be changed later. Anyway, this seems to return an empty list no matter what I try.
The second version was essentially the same, but relied on nested if statements instead of boolean operators:
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
This, too, returns an empty list. The list of words I am using comes from here. I'm assuming I am missing something obvious, since I'm new to working with external files. I should point out that these functions are prototypes of the former, and both work just fine:
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
EDIT: Just for reference, here is the code in its entirety (including commented-out sections of code.)
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
You are using the
string.lower
method, but you missed the()
.Without the parentheses, you are assigning not the value of the
.lower
function, but the function itself.EDIT: For the record (because I can't leave comments), your method of using
for i in fin
is fine, since files are iterable, delimited by\n
.