First attempts: Difference between revisions
No edit summary |
No edit summary |
||
Line 6: | Line 6: | ||
punct = set(string.punctuation + string.digits) | punct = set(string.punctuation + string.digits) | ||
longest = None | longest = None | ||
shortest = None | |||
text = raw_input('Enter some text:') | text = raw_input('Enter some text:') | ||
free_text = ''.join(x for x in text if x not in punct) | free_text = ''.join(x for x in text if x not in punct) | ||
Line 17: | Line 17: | ||
while longest is None or len(word) > len(longest): | while longest is None or len(word) > len(longest): | ||
longest = word | longest = word | ||
while | while shortest is None or len(word) < len(shortest): | ||
shortest = word | |||
if number < 1: | if number < 1: | ||
print 'number of words:', number | print 'number of words:', number | ||
Line 26: | Line 26: | ||
print 'number of words:', number | print 'number of words:', number | ||
print 'longest word is:', longest | print 'longest word is:', longest | ||
print 'shortest word is:', | print 'shortest word is:', shortest | ||
print 'avarage word lenght is:', float(summa)/number | print 'avarage word lenght is:', float(summa)/number | ||
print 'end of program' | print 'end of program' | ||
</source> | </source> |
Revision as of 19:44, 4 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:
summa = total + len(word)
total = summa
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:', summa
print 'number of words:', number
print 'longest word is:', longest
print 'shortest word is:', shortest
print 'avarage word lenght is:', float(summa)/number
print 'end of program'