title: How can I clean up this code so that it isn't as long and does not involve much copying and pasting?
body: <p>This code is for a hangman game and it works perfectly.It, however, is quite messy and long because I had to copy and paste a lot of the code over and over again. It works but is not the most functional way of doing this. How can I clean up this code to not have to copy and paste over and over again? I already tried using a for loop and a while loop but that just messed up the game and the game then didn't work properly. How can I improve this code?</p>
  
  <pre><code>import random
  
  def getWord():
      dictionary_file=open("dictionary.txt","r")
      lines=dictionary_file.readlines()
      words=[]
      for i in range(0,len(lines)-1):
          x=lines[i]
          z=len(x)
          a=x[:z-1]
          words.append(a)
      words.append(lines[i+1])
      game_word=random.choice(words)
      dictionary_file.close()
      return(game_word)
  
  def display(game_word):
      for i in range(len(game_word)):
          print('_ ', end='')
      print()
  
  def play(game_word):
      n = len(game_word)
      word = list("_"*n)
  
      guess1 = input("Letter: ")
      if guess1.isalpha():
          for j in range(n):
              if guess1 in game_word[j]:
                  word[j] = guess1 + " "
              else:
                  word[j] = "_ "
      print("".join(word))
  
      guess2 = input("Letter: ")
      if guess2.isalpha():
          for j in range(n):
              if word[j] == '_ ':
                  if guess2 in game_word[j]:
                      word[j] = guess2 + " "
                  else:
                      word[j] = "_ "
      print("".join(word))
      if '_ ' not in word:
          print("YOU WIN")
          return
  
      guess3 = input("Letter: ")
      if guess3.isalpha():
          for j in range(n):
              if word[j] == '_ ':
                  if guess3 in game_word[j]:
                      word[j] = guess3 + " "
                  else:
                      word[j] = "_ "
      print("".join(word))
      if '_ ' not in word:
          print("YOU WIN")
          return
  
      guess4 = input("Letter: ")
      if guess4.isalpha():
          for j in range(n):
              if word[j] == '_ ':
                  if guess4 in game_word[j]:
                      word[j] = guess4 + " "
                  else:
                      word[j] = "_ "
      print("".join(word))
      if '_ ' not in word:
          print("YOU WIN")
          return
  
      guess5 = input("Letter: ")
      if guess5.isalpha():
          for j in range(n):
              if word[j] == '_ ':
                  if guess5 in game_word[j]:
                      word[j] = guess5 + " "
                  else:
                      word[j] = "_ "
      print("".join(word))
      if '_ ' not in word:
          print("YOU WIN")
          return
          return
  
      guess6 = input("Letter: ")
      if guess6.isalpha():
          for j in range(n):
              if word[j] == '_ ':
                  if guess6 in game_word[j]:
                      word[j] = guess6 + " "
                  else:
                      word[j] = "_ "
      print("".join(word))
      if '_ ' not in word:
          print("YOU WIN")
          return
  
      guess7 = input("Letter: ")
      if guess7.isalpha():
          for j in range(n):
              if word[j] == '_ ':
                  if guess7 in game_word[j]:
                      word[j] = guess7 + " "
                  else:
                      word[j] = "_ "
      print("".join(word))
      if '_ ' not in word:
          print("YOU WIN")
          return
  
      guess8 = input("Letter: ")
      if guess8.isalpha():
          for j in range(n):
              if word[j] == '_ ':
                  if guess8 in game_word[j]:
                      word[j] = guess8 + " "
                  else:
                      word[j] = "_ "
      print("".join(word))
      if '_ ' not in word:
          print("YOU WIN")
          return
  
      guess9 = input("Letter: ")
      if guess9.isalpha():
          for j in range(n):
              if word[j] == '_ ':
                  if guess9 in game_word[j]:
                      word[j] = guess9 + " "
                  else:
                      word[j] = "_ "
      print("".join(word))
      if '_ ' not in word:
          print("YOU WIN")
          return
  
      guess10 = input("Letter: ")
      if guess10.isalpha():
          for j in range(n):
              if word[j] == '_ ':
                  if guess10 in game_word[j]:
                      word[j] = guess10 + " "
                  else:
                      word[j] = "_ "
      print("".join(word))
      if '_ ' not in word:
          print("YOU WIN")
          return
  
      guess11 = input("Letter: ")
      if guess11.isalpha():
          for j in range(n):
              if word[j] == '_ ':
                  if guess11 in game_word[j]:
                      word[j] = guess11 + " "
                  else:
                      word[j] = "_ "
      print("".join(word))
      if '_ ' not in word:
          print("YOU WIN")
          return
  
      guess12 = input("Letter: ")
      if guess12.isalpha():
          for j in range(n):
              if word[j] == '_ ':
                  if guess12 in game_word[j]:
                      word[j] = guess12 + " "
                  else:
                      word[j] = "_ "
      print("".join(word))
      if '_ ' not in word:
          print("YOU WIN")
          return
  
      guess13 = input("Letter: ")
      if guess13.isalpha():
          for j in range(n):
              if word[j] == '_ ':
                  if guess13 in game_word[j]:
                      word[j] = guess13 + " "
                  else:
                      word[j] = "_ "
      print("".join(word))
      if '_ ' not in word:
          print("YOU WIN")
          return
  
      guess14 = input("Letter: ")
      if guess14.isalpha():
          for j in range(n):
              if word[j] == '_ ':
                  if guess14 in game_word[j]:
                      word[j] = guess14 + " "
                  else:
                      word[j] = "_ "
      print("".join(word))
      if '_ ' not in word:
          print("YOU WIN")
          return
  
      guess15 = input("Letter: ")
      if guess15.isalpha():
          for j in range(n):
              if word[j] == '_ ':
                  if guess15 in game_word[j]:
                      word[j] = guess15 + " "
                  else:
                      word[j] = "_ "
      print("".join(word))
      if '_ ' not in word:
          print("YOU WIN")
          return
  
      print("YOU LOSE")
      print('Word was '+ game_word)
  
  game_word = getWord()
  display(game_word)
  play(game_word)
  </code></pre>