First attempts: Difference between revisions
No edit summary |
No edit summary |
||
Line 13: | Line 13: | ||
total = 0 | total = 0 | ||
for word in words: | for word in words: | ||
total = total + len(word) | |||
while longest is None or len(word) > len(longest): | while longest is None or len(word) > len(longest): | ||
longest = word | longest = word | ||
Line 23: | Line 22: | ||
print 'end of program' | print 'end of program' | ||
else: | else: | ||
print 'number of letters:', | print 'number of letters:', total | ||
print 'number of words:', number | print 'number of words:', number | ||
print 'longest word is:', longest, ',', len(longest) | print 'longest word is:', longest, ',', len(longest) | ||
print 'shortest word is:', shortest, ',', len(shortest) | print 'shortest word is:', shortest, ',', len(shortest) | ||
print 'avarage word lenght is:', float( | print 'avarage word lenght is:', float(total)/number | ||
print 'end of program' | print 'end of program' | ||
</source> | </source> |
Revision as of 22:21, 5 October 2015
OMG! My weird code works! (P.S. Week ago I didn't know Python exists)
import string
punct = set(string.punctuation + string.digits)
longest = None
shortest = None
text = raw_input('Enter some text:')
free_text = ''.join(x for x in text if x not in punct)
words = free_text.split()
number = len(words)
total = 0
for word in words:
total = total + len(word)
while longest is None or len(word) > len(longest):
longest = word
while shortest is None or len(word) < len(shortest):
shortest = word
if number < 1:
print 'number of words:', number
print 'end of program'
else:
print 'number of letters:', total
print 'number of words:', number
print 'longest word is:', longest, ',', len(longest)
print 'shortest word is:', shortest, ',', len(shortest)
print 'avarage word lenght is:', float(total)/number
print 'end of program'