First attempts: Difference between revisions

From XPUB & Lens-Based wiki
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)
import string
 
punct = set(string.punctuation)
<source lang="python">
longest = None
import string
smallest = None
punct = set(string.punctuation)
text = raw_input('Enter some text:')
longest = None
free_text = ''.join(x for x in text if x not in punct)
smallest = None
words = free_text.split()
text = raw_input('Enter some text:')
number = len(words)
free_text = ''.join(x for x in text if x not in punct)
total = 0
words = free_text.split()
for word in words:
number = len(words)
summa = total + len(word)
total = 0
total = summa
for word in words:
while longest is None or len(word) > len(longest):
summa = total + len(word)
longest = word
total = summa
while smallest is None or len(word) < len(smallest):
while longest is None or len(word) > len(longest):
smallest = word
longest = word
if number < 1:
while smallest is None or len(word) < len(smallest):
print 'number of words:', number
smallest = word
print 'end of program'
if number < 1:
else:
print 'number of words:', number
print 'number of letters:', summa
print 'end of program'
print 'number of words:', number
else:
print 'longest word is:', longest
print 'number of letters:', summa
print 'shortest word is:', smallest
print 'number of words:', number
print 'avarage word lenght is:', float(summa)/number
print 'longest word is:', longest
print 'end of program'
print 'shortest word is:', smallest
print 'avarage word lenght is:', float(summa)/number
print 'end of program'
</source>

Revision as of 16:03, 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'