First attempts: Difference between revisions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
OMG! My weird code works! (P.S. It works in Python 2.7) | OMG! My weird code works! (P.S. It works in Python 2.7)<br /> | ||
https://38.media.tumblr.com/d617c836e0d9f3473c916528f48bc54f/tumblr_nc4o8e06Ut1r93xiko1_r1_500.gif | |||
<source lang="python"> | <source lang="python"> | ||
Line 30: | Line 31: | ||
</source> | </source> | ||
But later on I figured out that there are a bunch of functions to write this code ten times shorter... |
Revision as of 16:59, 4 October 2015
OMG! My weird code works! (P.S. It works in Python 2.7)
import string
punct = set(string.punctuation)
longest = None
smallest = 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 smallest is None or len(word) < len(smallest):
smallest = 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:', smallest
print 'avarage word lenght is:', float(summa)/number
print 'end of program'
But later on I figured out that there are a bunch of functions to write this code ten times shorter...