Counting how many words are over a certain limit in a string Python
<p>Thank you all for help on previous part. I have now finished that.
However changing title slightly and rewording question I now say that this is my code.</p>

<pre><code>s = raw_input("Enter your text: ")

longestWord = max(s.split(), key=len)

k = list(s)

count = len(k)

wordsOver = []

over = count - 140

def numLen(s, n):
    return sum(1 for x in s.split() if len(x) &gt;= n)
    for x in s.split():
        if len(x) &gt;= n:
            wordsOver.insert(0, x)

val = numLen(s, 7)

if count &gt; 140:
    print ("Sorry, that is more than 140 characters.")
    print ("You had a total of " + str(count) + " characters.")
    print ("That's " + str(over) + " over the max allowed.")
    print ("You're longest word was, " + longestWord)
    print ("There are " + str(val) + " words over 7 characters.")
    print ("They were:")
    print (wordsOver)
    print ("You may want to consider changing them for shorter words.")
else:
    print ("That's short enough!")
</code></pre>

<p>So now what I'm looking for is why the displaying of the words that are over isn't working, why and how to fix it. BTW for a little help it's the wordsOver bit that's broken</p>

<p>Welcome to SO! </p>

<p>I think this is what you want to do. In your numLen function you need to use <code>append()</code> rather than <code>insert()</code> when adding to your list. This is because when using insert you're not incrementing the index so each time you insert at index 0 you overwrite any value that's already there. Append does the legwork of finding out where the end of your list is and puts what you pass to it onto the end.</p>

<pre><code>s = raw_input("Enter your text: ")

longestWord = max(s.split(), key=len)

k = list(s)

count = len(k)

wordsOver = []

over = count - 140

def numLen(s, n):
    for x in s.split():
        if len(x) &gt;= 7:
            wordsOver.append(x)
    return len(wordsOver)

val = numLen(s, 7)

if count &gt; 140:
    print ("Sorry, that is more than 140 characters.")
    print ("You had a total of " + str(count) + " characters.")
    print ("That's " + str(over) + " over the max allowed.")
    print ("You're longest word was, \"" + longestWord + "\"")
    print ("There are " + str(val) + " words over 7 characters.")
    print ("They were:")
    print (wordsOver)
    print ("You may want to consider changing them for shorter words.")
else:
    print ("That's short enough!")
</code></pre>

python python-2.7