forked from Jackevansevo/Udacity-Python-Code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_words.py
More file actions
34 lines (24 loc) · 951 Bytes
/
count_words.py
File metadata and controls
34 lines (24 loc) · 951 Bytes
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
# Write a procedure, count_words, which takes as input a string
# and returns the number of words in the string. You may consider words
# as strings of characters separated by spaces.
def count_words(passage):
# Reject empty list
if passage == (""):
return 0
# instead of counting the number of words count the number of whitespace
whitespaces = 0
for c in passage:
# add to the count of spaces
if c == " ":
whitespaces += 1
return whitespaces + 1
# Add 1 onto the number of whitespaces for the trailing whitespace at end of passage
passage =("The number of orderings of the 52 cards in a deck of cards " # 13
"is so great that if every one of the almost 7 billion people alive " #
"today dealt one ordering of the cards per second, it would take " #
"2.5 * 10**40 times the age of the universe to order the cards in every " #
"possible way.") # 2
print count_words(passage)
#>>>56
quote = ("")
print count_words(quote)