Python: Absorbing newlines on keyboard input
Question: 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.

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.

The output I am getting is this:

    
    
    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): 
    

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.

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.

    
    
    #
    # 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
    


Answer: `input()` doesn't accept multiple lines. Each call to `input()` retrieves
exactly one line.

So, you have the first call to input:

    
    
        s = input("Enter a string: ")
    

receives this line:

    
    
    def countLetters():
    

Sure enough, you can see it has three T's, three E's, and so forth.

The next call to `input()` is this:

    
    
        ans = input("\nContinue (yes/no): ").replace(" ", "").lower()
    

to which you have typed:

    
    
    s = input("Enter a string: ")
    

Since your response is neither `'yes'` nor `'no'`, your loop asks again. This
time you type:

    
    
    d = histogram(s.upper())
    

which is also neither `'yes'` nor `'no'`.

This pattern continues until you get tired of typing nonsense to the question.
Finally, you type "no" and the game ends.

If you want to read more than one line at a time, try `sys.stdin.read()` or
perhaps `sys.stdin.readlines()`. For example:

    
    
    import sys
    def countLetters():
        print("Enter several lines, followed by the EOF signal (^D or ^Z)")
        s = sys.stdin.read()
        d = histogram(s.upper())
        printSortedDict(d, True)
    

