skillet
skillet

Reputation: 5

word guessing game in python

I'm trying to create a guessing game that prompts the user for a word, then prompts for letter guesses. I was having trouble with my code - my expected output was " guess 'not found'" if the letter guess was not in the word, but no matter the guess, the output continues to be " guess 'found'". Here's my original code:

word = input('Enter a guess word: ')
while True:
        guess = input('Enter a guess letter: ')
        for word in guess:
                if guess in word:
                        print('\t>', guess, 'found')
                else:
                        print('\t>', guess, 'not found')

for example if the user inputs 'hello' as the word and 'p' as the guess, it will still print '> p found'. Later, I was just fooling around and it worked? Firstly, when I tried to apply this to my original code, it didn't work (the problem remained, just that the output continuously returned 'not found' instead). Secondly, I do not understand how this ended up working... This is the working(?) code:

for x in 'word':
        pass
for word in x:
        if 'p' in word:
                print(':)')
        else:
                print(':(')

attempt to apply to original code:

word = input('Enter a guess word: ')
while True:
        guess = input('Enter a guess letter: ')
        for x in word:
                pass
        for word in x:
                if guess in word:
                        print('\t>', guess, 'found')
                else:
                        print('\t>', guess, 'not found')

Upvotes: 0

Views: 1200

Answers (4)

devReddit
devReddit

Reputation: 2947

In your code, you first defined word as a variable and then stored the input in it.

word = input('Enter a guess word: ')

Then, you tried to execute the while loop there, you're taking the input letter in guess:

guess = input('Enter a guess letter: ')
    

The problem begins from the next line, inside this loop:

for word in guess:
        if guess in word:
                print('\t>', guess, 'found')
        else:
                print('\t>', guess, 'not found')

In the loop, the iteration condition is for word in guess, which considers word as a local item inside the loop containing each element of the guess per iteration.

In next line, the if condition checks if guess in word, which means, you're checking if the guess is in the word, but this word is nothing but the word introduced in for loop. So, technically you've overwritten the actual word introduced at the beginning, with the letters from the guess input.

As the guess from input and the word in guess are same, this condition is satisfied for the letter p. Hence you're getting the p found as the result.

But it should be changed too, take each letter from guess, and check whether the letter is in word. Just change the name of the variable word in the condition for word in guess to something like for letter in guess. Also change if guess in word to if letter in word, while you're running a loop for letter in guess. And it'll work properly:

word = input('Enter a guess word: ')
while True:
    guess = input('Enter a guess letter: ')
    for letter in guess:
            if letter in word:
                    print('\t>', guess, 'found')
            else:
                    print('\t>', guess, 'not found')

Upvotes: 0

Alexandre Marcq
Alexandre Marcq

Reputation: 365

You have to be careful when naming your variables. You first declare word as your target but then use word again to represent each character of guess.

Let's say my guess word is test and my guess is a.

Since you wrote for word in guess, word is now a. That is why if word in guess will always return True, because a is indeed in a.

Here is how to fix it:

word = input('Enter a guess word: ')
while True:
        guess = input('Enter a guess letter: ')
        for x in guess: # or any other variable name besides "word"
                if x in word:
                        print('\t>', guess, 'found')
                else:
                        print('\t>', guess, 'not found')

Upvotes: 0

user15801675
user15801675

Reputation:

You are basically over writing the variable word with the letter when you iterate over the loop. Also, you need to iterate over word and not guess:

word = input('Enter a guess word: ')
while True:
        guess = input('Enter a guess letter: ')
        for words in word:
                
                if guess==words:
                        print('\t>', guess, 'found')
                        break
                else:
                        print('\t>', guess, 'not found')

Upvotes: 0

ArmandoKeller
ArmandoKeller

Reputation: 11

You are overwriting the value of the word variable with the following line:

 for word in guess:

change the variable name

Upvotes: 1

Related Questions