Python 纵横字谜解决同伴
我正在写一个程序,它可以记录字母和索引,并给出填字游戏的答案(不是填字游戏解决方案,而是一个帮助解决填字游戏的工具,如果有意义的话) 我已经编写了两个版本的算法,但两个版本似乎都不正确。我第一次尝试的是:Python 纵横字谜解决同伴,python,crossword,Python,Crossword,我正在写一个程序,它可以记录字母和索引,并给出填字游戏的答案(不是填字游戏解决方案,而是一个帮助解决填字游戏的工具,如果有意义的话) 我已经编写了两个版本的算法,但两个版本似乎都不正确。我第一次尝试的是: fin = open('words.txt') def answer_finder(): global fin possible_answers = [] length = int(raw_input("How long is the word?")) let
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 = file('words.txt').readlines ()
您正在使用
string.lower
方法,但是您错过了()
字母3=原始输入(“您知道的第三个字母是什么?”)。降低
你知道的第三个字母是什么
>>>信3
>>>字母3=原始输入(“您知道的第三个字母是什么?”)。lower()
你知道的第三个字母是什么
>>>信3
“a”
如果没有括号,则指定的不是.lower
函数的值,而是函数本身
编辑:对于记录(因为我不能留下注释),您在fin中使用I的
的方法很好,因为文件是可编辑的,由\n
分隔,您似乎没有将word文件拆分(甚至从中读取)…这是在您没有发布的代码中完成的吗?哦,可能吧。所有这些函数都在同一个文档中,我在顶部有fin=open('words.txt')
。然后我在每个函数中调用global fin
。我仍然非常熟悉通过Python读取文件,所以如果这不是应该的方式,请告诉我。到目前为止,我添加了该程序的完整代码以供参考。这使得代码的加载速度非常慢,answer\u finder
仍然返回一个空列表。有没有一种方法我应该在函数中调用来读取这些行?如果是这样的话,为什么第一个字母长度查找器(我真的需要修正这个名字)在输入更少的情况下做基本相同的事情时还能工作呢?哦。伙计。好眼睛。我一定读过一百遍了。非常感谢。代码现在运行得很好。这两个版本的算法在性能方面是否有差异?嵌套ifs与布尔运算符?最奇怪的是,这两种方法都不重要。算法性能没有太大差别,因为最重的东西实际上是通过单词文件,这两种方法都至少执行一次。
fin = file('words.txt').readlines ()
>>> 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'