-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordCounter.py
More file actions
46 lines (43 loc) · 1.62 KB
/
WordCounter.py
File metadata and controls
46 lines (43 loc) · 1.62 KB
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
letters=0
words=0
sentences=0
worddeviders=(","," ","-",":",";","'",'"',"/")
sentencedeviders=(".","?","!")
contents=" "
success=False
def getfilename():
global success
attempts=0
while (not success) and (attempts<5):
try:
filename=input("Enter the text file examined, including its extension, e.g. filename.txt: ")
datafile=open(filename,"r")
success=True
except IOError:
print("Specified file cannot be found or opened.",end="")
attempts=attempts+1
if attempts<5:
print(" Try again.")
else:
print()
datafile=""
return datafile
file=getfilename()
if not success:
print("Too many attempts! Good-bye!")
else:
while not contents=="":
contents=file.readline()
contents=contents.strip('\n')
for k in range(0,len(contents)):
if contents[k].isalnum():
letters=letters+1
elif (contents[k] in worddeviders) and (contents[k-1] not in sentencedeviders) and (contents[k-1] not in worddeviders):
words=words+1
elif (contents[k] in sentencedeviders) and (contents[k-1] not in sentencedeviders) and (contents[k-1] not in worddeviders):
sentences=sentences+1
words=words+1
elif (contents[k] in sentencedeviders) and (contents[k-1] not in sentencedeviders) and (contents[k-1] in worddeviders):
sentences=sentences+1
print("Sentences: ",sentences,"\nWords: ",words,"\nLetters: ",letters)
file.close()