奇数Python逻辑错误
如果这只是我的一个愚蠢的失误,我很抱歉,但我对Python相对缺乏经验,我不明白为什么这不起作用 我有一个名为奇数Python逻辑错误,python,oop,Python,Oop,如果这只是我的一个愚蠢的失误,我很抱歉,但我对Python相对缺乏经验,我不明白为什么这不起作用 我有一个名为Game的类,它包含一个从文本文件读入的word\u列表。该类的方法之一如下所示: def make_guess(self, guess): print("Guess: ", guess) if guess == self.target_word: print("Got it!") if guess in self.word
Game
的类,它包含一个从文本文件读入的word\u列表
。该类的方法之一如下所示:
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")
无论我给它什么样的输入,如果在self.word列表中猜测,我都无法使它在路径中绊倒。我试着比较变量的类型(列表中的每个单词和我的输入),但它们在我看来是相同的
类的整个定义(如果有帮助):
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")
即使我故意猜一个我知道在列表中的单词,它也不会说这个单词实际上在列表中。我犯了什么愚蠢的错误?(如果你能指出我可以更坚持Python风格的方法,我也会很感激的!)在你说的那句话中
for word in self.dictionary:
这将从文本文件中读取整行。因此,变量单词不引用文本文件中的单词。您应该首先从文本文件中读取一行,然后从该行中提取单个单词。像这样:
for line in self.dictionary:
words=line.split();
for word in words:
您需要从
wordsEn.txt
的行中删除换行符。之后
for word in self.dictionary:
插入:
word = word.rstrip()
我假设
wordsEn.txt
的每一行都列出了一个单词。不是将整行添加到self.word\u列表中,而是通过调用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")
输入的值和self.word\u list
的值是多少?即使该行运行,也会立即丢弃num\u right
局部变量。这不是在字符串中迭代单词的方式for word in line
迭代字符。在任何情况下,文件格式可能是每行一个单词,如果不是,正确提取单词将取决于对格式的了解。OP似乎在使用Python3(这行print(“Target:,self.Target\u word)
在Python2和Python3中的行为将特别不同),在这种情况下,input
的行为类似于旧的raw\u input
。这非常有效,谢谢。rstrip()看起来是一个有用的函数。谢谢你让我知道这件事@lvc感谢您的评论。我删除了我的原始输入。
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")
>>> 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'
>>>