rmotr-curriculum / post-your-question-questions

0 stars 0 forks source link

hangman game #129

Closed selmabensaid closed 5 years ago

selmabensaid commented 5 years ago

I am not passing the last test of the hangman game. the error message says: Did not raise game finished exception. Can you please help me understand why?

Assignment: http://learn.rmotr.com/python/post-a-question/post-your/questions

User code:

def guess_letter(game, letter):
    answer_word=game['answer_word']
    masked_word=game['masked_word']
    uncover_word=_uncover_word(game['answer_word'], masked_word, letter)
    remaining_misses=game['remaining_misses']
    if answer_word==masked_word or remaining_misses==0:
        raise GameFinishedException()
    if answer_word==uncover_word:
        raise GameWonException()
    if game['remaining_misses']==1 and letter not in answer_word:
        raise GameLostException 

    game['masked_word']=uncover_word
    (game['previous_guesses']).append(letter.lower())

    if letter.lower() not in game['answer_word'].lower():
        (game['remaining_misses'])-=1

    return game   
jsymons commented 5 years ago

This is because of the order of your logic:

    game['masked_word']=uncover_word
    (game['previous_guesses']).append(letter.lower())

    if letter.lower() not in game['answer_word'].lower():
        (game['remaining_misses'])-=1

    return game   

All of that is placed after your code that raises GameWonException/GameLostException, as a result if one of those exceptions is raised none of that code is executed. What that means is that this code:

if answer_word==masked_word or remaining_misses==0:
        raise GameFinishedException()

will never be able to have the second half of the or be true, because your code never reaches a state where remaining_misses would equal 0. (If it's 1 and what they guess isn't in answer_word you raise GameLostException, the code that decrements remaining_misses to zero is never reached)