0
def isWordGuessed(secretWord, lettersGuessed):
    '''
    secretWord: string, the word the user is guessing
    lettersGuessed: list, what letters have been guessed so far
    returns: boolean, True if all the letters of secretWord are in lettersGuessed;
      False otherwise
    '''
    score = 0
    tot = len(secretWord)
    for i in range(len(secretWord)):
       for j in range(len(lettersGuessed)):
           if (secretWord[i] == lettersGuessed[j]):
               score +=1
    if score == tot:
        return True 
    else:
         return False

For most of the words and guess letter m getting correct answer but while providing sceretWord as mangosteen and letter guessed as ['z', 'x', 'q', 'm', 'a', 'n', 'g', 'o', 's', 't', 'e', 'e', 'n'] I am getting wrong output.

Any suggestions, why?

0

4 Answers 4

2

Looks like you could use:

set(secretWord).issubset(lettersGuessed)

to determine if guessed includes all of the letters in secret word.

Sign up to request clarification or add additional context in comments.

2 Comments

Was just typing the same thing, +1 to you :)
well this is way indeed but i was trying not to use any string commands to perform this task , thanks for help anyway
0

My final answer should work with repeated characters, misguessed characters, etc. The key is that each character of the word should be removed from the list of guessed characters; if that fails, the character wasn't guessed (often enough) and we should return False.

def isWordGuessed(secretWord, lettersGuessed):
    guesses_left = list(lettersGuessed)
    for character in secretWord:
        try:
            guesses_left.remove(character)
        except ValueError:
            return False  # remove failed
    return True  # all characters succeeded

1 Comment

i am new to coding in python so no clue about try and except , i guess it is used for exception anyways thanks :)
0

Here is why:

There are two same character in mangosteen, e. Your code encountered it twice, so it adds two score. The total score of when secretWord[i] and lettersGuessed[j] is e, adds up to 4.

Here is a solution:

You can simply use the set to check if every unique letters is there or not:

return all([True if i in set(g) else False for i in set(w)])

This makes the function much shorter :)

Demo:

>>> g = ['z', 'x', 'q', 'm', 'a', 'n', 'g', 'o', 's', 't', 'e', 'e', 'n']
>>> w = 'mangosteen'
>>> score = 0
>>> all([i for i in set(w) if i in set(g)])    
True

An alternative is to break the inner loop straight after you found the wanted letter:

if secretWord[i] == lettersGuessed[j]
    score +=1
    break

Hope this helps!

9 Comments

i thought so but checking the value of score every time reveled that is getting incremented for every value it is encountering , and i am out of reason as of why it is happening
That's because the inner loop meet e twice for each e in the outer loop. Thus, the score is doubled.
thank alot mate it seems breaking the for loop after encountering the condition solves the problem
That's very true! Once you meet the wanted char, you can just break the inner loop.
But this also fails: if the word is "mangosteen" and the guess is "mmangosten", the score will be 10.
|
0
def isWordGuessed(secretWord, lettersGuessed):
lst = []
for e in lettersGuessed:
    if e in secretWord:
        lst.append(e)
if len(lst) == len(secretWord):
    return True
return False

I did this and it passed all cases.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.