Jordan Fagan Jordan Fagan - 1 year ago 62
Python Question

Counting how many words are over a certain limit in a string Python

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.

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) >= n)
for x in s.split():
if len(x) >= 7:
wordsOver.insert(0, x)

val = numLen(s, 7)

if count > 140:
print ("Sorry, that is more than 140 characters.")
print ("You had a total of " + 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 " + wordsOver)
print ("You may want to consider changing them for shorter words.")
print ("That's short enough!")

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

Answer Source

in the for loop you're looking at each character and checking if that character i is a character that is in an alphabet using .isalpha() which will return false when it encounters a space since it is not alphabetic.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download