-
Notifications
You must be signed in to change notification settings - Fork 1
/
count_words.py
61 lines (45 loc) · 1 KB
/
count_words.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
Problem
-------
**Count Words in a String**
Counts the number of individual words in a string.
For added complexity read these strings in from a
text file and generate a summary.
Solution
--------
The solution is provided using a simple string split
Author
------
dbonadiman
"""
import sys
def words(s):
"""
words
This function simpli split the string in spaces
and counts the number of splits, it's a very
simple and sometimes wrong.
Parameters:
s ==> the string from wich to count the vowels
Test:
>>> words('ciao come va')
3
>>> words('cat')
1
"""
return len(s.split(" "))
def main():
try:
print("\nThis program counts the words in a string\n"
"Please enter the string: \n")
string = raw_input("--> ")
print(words(string))
return 0
except Exception, e:
print(e)
return 1
if __name__ == "__main__":
import doctest
doctest.testmod()
status = main()
sys.exit(status)