1

i am a beginner in python and would appreciate your help.

I need to define a function(part of the HANGMAN GAME):

def show_hidden_word(secret_word, old_letters_guessed):

secret_word = a string of the word the player needs to guess. old_letters_guessed = a list that contains the guessed letters by the player thus far.

the function needs to return a string made from letters and ' _ '. the string shows the letters from the old_letters_guessed list that are in the secret_word string in their right place(index), and the rest of the letters the player has not guessed yet as a ' _ '.

its supposed to look like this:

>>> secret_word = "mammals"
>>> old_letters_guessed = ['s', 'p', 'j', 'i', 'm', 'k']
>>> print(show_hidden_word(secret_word, old_letters_guessed))
m _ m m _ _ s

this is what ive tried to do and it doesn't work (has to be done using for/ while loop):

def show_hidden_word(secret_word, old_letters_guessed):
 clue = ""
 for letter in secret_word:
  if letter in clue == True:
   clue + letter
   clue = clue + letter
  else:
   clue + '_'
   clue = '_'+ clue
 return clue

Thank you very much!

2
  • The "== True" in if-condition should be removed. Due to the operator priority it will always evaluate to false. The two lines with "something + something" don't do anything (result of addition is not used). Commented Jul 14, 2022 at 9:30
  • Generally, when asking here you shouldn't just write "doesn't work" but describe what happens, what should happen and show the full error message (if any) as properly formatted text in the question. Commented Jul 14, 2022 at 9:37

2 Answers 2

0

This should do the trick:

def show_hidden_word(secret_word, old_letters_guessed):
  return " ".join([c if c in old_letters_guessed else "_" for c in secret_word])

secret_word = "mammals"
old_letters_guessed = ['s', 'p', 'j', 'i', 'm', 'k']
print(show_hidden_word(secret_word, old_letters_guessed))
Sign up to request clarification or add additional context in comments.

Comments

0

There are a few issues in your code:

  • clue = '_' + clue should be clue = clue + '_', since you want to append the underscore, not prepend it. You did it right for the other case, where you have clue = clue + letter

  • The if condition is wrong in two ways:

    • It should not have the comparison with True, as this will first compare the string with True, and only then perform the in operator.
    • You don't want to check whether the letter is in clue (which is the result you are building), but whether it is in the old guesses.

    So it should be: if letter in old_letters_guessed:

  • Not a blocking issue, but the stand-alone expressions clue + letter and clue + '_' do nothing useful. These lines of code can be removed.

If you fix those issues, your code will work.

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.