我正在编写一个程序,它接受字母和索引并吐出填字游戏答案(不是填字游戏解算器,而是一个帮助解决填字游戏的工具,如果有意义的话)。
我已经编写了两个版本的算法,但似乎都没有正常工作。我试过的第一个就是:
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
这也会返回一个空列表。我使用的单词列表来自here。我假设我遗漏了一些明显的东西,因为我刚开始使用外部文件。我应该指出,这些函数是前者的原型,两者都可以正常工作:
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
答案 0 :(得分: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
分隔。
答案 1 :(得分:0)
在尝试使用数据之前,您需要先从文件中读取数据。
fin = file('words.txt').readlines ()