奇怪的Python逻辑错误

时间:2014-06-12 05:05:56

标签: python oop

如果这对我来说只是一个愚蠢的错误,我道歉,但我对Python相对缺乏经验,我无法弄清楚为什么这不起作用。

我有一个名为Game的类,其中包含一个已从文本文件中读入的word_list。该课程的方法之一如下:

def make_guess(self, guess):
    print("Guess:     ", guess)        
    if guess == self.target_word:
        print("Got it!")
    if guess in self.word_list:
        num_right = self.compare_letters(guess, self.target_word)
    else:
        print("Not valid guess; not in list")

无论我给出什么输入,我都永远无法让它走if guess in self.word_list路径。我尝试比较变量的类型(列表中的每个单词和我的输入),但它们对我来说似乎是一样的。

该类的整个定义是否有帮助:

class Game:    
def __init__(self, difficulty):
    self.difficulty = difficulty
    if 0 < difficulty < 3:
        self.remaining_guesses = 5
    elif 3 <= difficulty < 5:
        self.remaining_guesses = 4
    else:
        self.remaining_guesses = 3

    self.word_list = []

    self.dictionary = open("wordsEn.txt")
    for word in self.dictionary:
        percent = int(floor(1000*random()))
        if len(word) == 6 and percent < 2:
            self.word_list.append(word)
    self.dictionary.close()

    percent = int(floor(len(self.word_list)*random()))
    self.target_word = self.word_list[percent]

def make_guess(self, guess):
    print("Guess:     ", guess)
    if guess == self.target_word:
        print("Got it!")
    if guess in self.word_list:
        num_right = self.compare_letters(guess, self.target_word)
    else:
        print("Not valid guess; not in list")


def display_word_list(self):
    print("in display")
    print(self.remaining_guesses)
    for word in self.word_list:
        print(word)
    print("Target: ", self.target_word)

def compare_letters(self, guess, target_word):
    for letter in guess:
        if letter == letter:
            print("yes")

`

主要是,我有:

new_game = Game(difficulty)
guess = input("Guess: ")
new_game.make_guess(guess)

即使我故意猜测一个我知道在列表中的单词,它也从未说过这个单词实际上在列表中。我犯的是什么愚蠢的错误? (如果你能指出我可以更多地遵循Python风格的方式,那也将受到赞赏!)

3 个答案:

答案 0 :(得分:1)

您需要从wordsEn.txt的行中删除换行符。之后

for word in self.dictionary:

插入:

    word = word.rstrip()

我假设wordsEn.txt的每一行都列出了一个单词。

答案 1 :(得分:0)

在你说的行

for word in self.dictionary:

这将从文本文件中读取整行。因此,变量词不会引用文本文件中的单词。您应该首先从文本文件中读取一行,然后从该行中获取单个单词。像这样:

for line in self.dictionary:
 words=line.split();
 for word in words:

答案 2 :(得分:0)

不是将整行添加到self.word_list,而是通过调用self.dictionary = open("wordsEn.txt").read().split()来添加每个单词。这是您编辑的课程:

class Game:
    def __init__(self, difficulty):
        self.difficulty = difficulty
        if 0 < difficulty < 3:
            self.remaining_guesses = 5
        elif 3 <= difficulty < 5:
            self.remaining_guesses = 4
        else:
            self.remaining_guesses = 3

        self.word_list = []

        self.dictionary = open("wordsEn.txt").read().split()
        for word in self.dictionary:
            percent = int(floor(1000*random()))
            if len(word) == 6 and percent < 2:
                self.word_list.append(word)
        self.dictionary.close()

        percent = int(floor(len(self.word_list)*random()))
        self.target_word = self.word_list[percent]

    def make_guess(self, guess):
        print("Guess:     ", guess)
        if guess == self.target_word:
            print("Got it!")
        if guess in self.word_list:
            num_right = self.compare_letters(guess, self.target_word)
        else:
            print("Not valid guess; not in list")


    def display_word_list(self):
        print("in display")
        print(self.remaining_guesses)
        for word in self.word_list:
            print(word)
        print("Target: ", self.target_word)

    def compare_letters(self, guess, target_word):
        for letter in guess:
            if letter == letter:
                print("yes")

展示.read().split()的上述概念:

>>> file = open('blah.txt')
>>> for word in file:
...     word
... 
'Hello,\n'
'\n'
'These\n'
'Are\n'
'Some\n'
'Vocabulary\n'
'Words\n'
'\n'
'Regards,\n'
'Me\n'
>>> file = open('blah.txt').read().split()
>>> for word in file:
...     word
... 
'Hello,'
'These'
'Are'
'Some'
'Vocabulary'
'Words'
'Regards,'
'Me'
>>>