title: 10.1.3 Guess the Word, Part 2 Help Needed
body: <p>I am currently stuck on exercise 10.1.3 on CodeHS, which is essentially a part in making a hangman game, without the hangman. I have my code nearly complete for this section of the game, yet for some reason whenever new input is given (for a letter), it clears previous input.</p>
  <p>Here is my code:</p>
  <pre><code>secret_word = &quot;character&quot;
  dashes = &quot;-&quot; * len(secret_word)
  print(dashes)
  #Prints the first row of dashes to indicate word length
  
  def get_guess():
      while True:
          guess = input(&quot;Guess: &quot;)
          if isinstance(guess, int):
              print(&quot;Your guess must be a lowercase letter.&quot;)
          else:
              if guess.islower():
                  if len(guess) == 1:
                      return guess
                  else:
                      print(&quot;Your guess must have exactly one character.&quot;)
  
  def update_dashes(secret_word, dashes, guess):
      result = &quot;&quot;
      for i in range(len(secret_word)):
          if secret_word[i] == guess:
              result = result[:i] + guess + result[i + 1:]
          else:
              result = result + &quot;-&quot;
      return result
  
  while True:
      guess = get_guess()
      if guess in secret_word:
          print(&quot;The letter is in the word.&quot;)
      else:
          print(&quot;That letter is not in the word.&quot;)
      print(update_dashes(secret_word, dashes, guess))
  #We believe the issue is here, it is printing the full set each time this loops.
  
  </code></pre>
  <p>When run, this is the output:</p>
  <pre><code>---------
  Guess: c
  The letter is in the word.
  c----c---
  Guess: h
  The letter is in the word.
  -h-------
  Guess: 
  </code></pre>
  <p>The code should be adding the &quot;h&quot; to the word, yet it is 'overwriting' what is already there.</p>
  <p>This is what I want it to print:</p>
  <pre><code>---------
  Guess: c
  The letter is in the word.
  c----c---
  Guess: h
  The letter is in the word.
  ch---c---
  Guess: 
  </code></pre>
  <p>Note that the word that should be guessed is &quot;character&quot;.</p>
  <p>And finally, this is the part of the assignment that relates to what the current problem is:</p>
  <blockquote>
  <p><strong>Step 2 - Update the Dashes</strong></p>
  <p>Here’s the tricky part. Write a function called update_dashes that
  takes three string arguments - the secret word, the current state of
  dashes, and the most recent guess - and returns the new state of
  dashes.</p>
  <p>So, for example, if the word is &quot;eggplant&quot;, the current value of
  dashes is &quot;e-------&quot;, and the guess is &quot;g&quot;, the function should return
  &quot;egg-----&quot;.</p>
  <p>If the word is &quot;eggplant&quot;, the current value of dashes is &quot;e-------&quot;,
  and the guess is &quot;z&quot;, the function should return &quot;e-------&quot; (nothing
  changes, since the guess was incorrect).</p>
  <p>Here’s how you might go about this.</p>
  <p>In your function, start with an empty result string. Write a for loop
  from 0 up to but not including the length of your secret word. Say you
  use the variable i in your for loop. Compare the character in
  secret_word at index i to the guess. If they’re equal, then that means
  the guess matches that letter in the word, so you should add the guess
  to the result. Otherwise, the guess does not match that letter in the
  word, so you should add whatever was at index i in dashes to your
  result.</p>
  <p><em>Wait, why don’t I just add a dash in the second case?</em></p>
  <p>If you just add a dash whenever the guess doesn’t match the character
  in secret_word at index i, that will wipe out any correct guesses the
  user has already made! Imagine the case where the word is “eggplant”,
  the state of dashes is “e——-“, and the guess is “g”. If you always add
  a dash when the guess doesn’t match the character in secret_word at
  index i, the result would be “-gg—–”. Suddenly, the “e” is gone,
  because “g” did not match “e”! By instead using dashes at index i, you
  might append either a letter or a dash, depending on whether or not
  the user had already guessed that letter prior to the current guess.</p>
  <p>Once your for loop is done, your result string should have letters and
  dashes in all the right places, so you can just return it!</p>
  </blockquote>