GrahamGoudeau
GrahamGoudeau

Reputation: 137

Odd Python logic error

I apologize if this is just a dumb slip-up on my part, but I am relatively inexperienced with Python and I can't figure out why this isn't working.

I have a class called Game, which contains a word_list that has been read in from a text file. One of the method of the class is as follows:

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")

No matter what input I give it, I can never make it trip the if guess in self.word_list path. I tried comparing the type of the variables (each word in the list, and my input), but they appeared to be the same to me.

The whole definition of the class if it helps:

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")

`

In main, I have:

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

Even if I deliberately guess a word that I know to be in the list, it never says that the word is in fact in the list. What stupid mistake am I making? (and if you could point out ways I could adhere more to the Python style, that would be appreciated as well!)

Upvotes: 0

Views: 98

Answers (3)

jrennie
jrennie

Reputation: 1957

You need to strip newlines from lines of wordsEn.txt. After

for word in self.dictionary:

insert:

    word = word.rstrip()

I'm assuming that each line of wordsEn.txt lists a single word.

Upvotes: 1

user3313129
user3313129

Reputation:

In the line where you say

for word in self.dictionary:

This reads an entire line from the text file. So the variable word doesn't refer to a word in the text file. You should first read a line from the text file and then take individual words from the line. Like this :

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

Upvotes: 0

A.J. Uppal
A.J. Uppal

Reputation: 19264

Instead of adding the full line to self.word_list, add each word by calling self.dictionary = open("wordsEn.txt").read().split() instead. Here is your edited class:

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")

Demonstrating the above concept of .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'
>>> 

Upvotes: 0

Related Questions