title: Python: Absorbing newlines on keyboard input
body: <p>I have a question specific to multi-line input from the keyboard.  It appears that my program is not absorbing newlines.  The effect is that, after the input is processed, the program appears to believe that there are a number of carriage returns pending equal to the number of newline characters in the input.</p>
  
  <p>I have looked at the sys module (sys.stdin.flush()), the msvsrc module (msvcrt.kbhit() and msvcrt.getch()), raw_input, and tried everything I can think of in search, but I'm coming up dry.  Perhaps strip() will work, but I can't seem to figure it out for the general case.</p>
  
  <p>The output I am getting is this:</p>
  
  <pre><code>Enter a string: def countLetters():
      s = input("Enter a string: ")
      d = histogram(s.upper())
      printSortedDict(d, True)
  ('T', 3)
  ('E', 3)
  ('F', 1)
  ('D', 1)
  (' ', 1)
  (':', 1)
  ('R', 1)
  ('(', 1)
  ('N', 1)
  (')', 1)
  ('L', 1)
  ('U', 1)
  ('C', 1)
  ('O', 1)
  ('S', 1)
  
  Continue (yes/no): 
  Continue (yes/no): 
  Continue (yes/no): 
  Continue (yes/no): 
  </code></pre>
  
  <p>I would like for the output to only have one "Continue (yes/no):" displayed.  It appears that the input() routine is eating the last newline (as expected) but not any of the intermediate newlines.  These newlines appear to be interpreted as input to the "Continue (yes/no):" statement.</p>
  
  <p>I am using Python 3.4.1.  I am developing on Win8 but would like the solution to run on at least Linux as well (in case the solution is platform specific).  Here is the program.  The easiest way to see the problem is just to copy the source code and past it as input to the program.</p>
  
  <pre><code>#
  # letterCountDict - count letters of input.  uses a dict()
  #
  """letterCountDict - enter a string and the count of each character is returned """
  
  # stolen from p. 122 of "Python for Software Design" by Allen B. Downey
  def histogram(s):
      """builds a histogram from the characters of string s"""
      d = dict()
      for c in s:
          if c in d:
              d[c] += 1
          else:
              d[c] = 1
      return d
  
  def printSortedDict(d, r=False):
      """sort and print a doctionary.  r == True indicates reserve sort."""
      sl = sorted(d.items(), key=lambda x: x[1], reverse=r)
      for i in range(len(sl)):
          print(sl[i])
  
  def countLetters():
      s = input("Enter a string: ")
      d = histogram(s.upper())
      printSortedDict(d, True)
  
  answerList = ["yes", "no"]
  done = False
  while not done:
      countLetters()
      ans = ""
      while ans not in answerList:
          ans = input("\nContinue (yes/no): ").replace(" ", "").lower()
      if ans == "no":
          done = True
  </code></pre>