Word Counter In Python

PHOTO EMBED

Mon Jul 17 2023 13:05:19 GMT+0000 (Coordinated Universal Time)

Saved by @pythonHub #python #python-hub #day16

def WordCounter(text:str) -> int:
    # getting sentences
    lines = text.split("\n")

    words = []
    for line in lines:
        # words from sentences
        print(line)
        for word in line.split(" "):
            words.append(word)
    return len(words)

# taking multiline input
lines = []
print("Please Enter your text: ")
while True:
    line = input()
    if not line:
        break
    lines.append(line)

# providing the string of input to the WordCounter function
words = WordCounter(str(lines))
print(f'Your text has {words} words.')
content_copyCOPY

https://python-hub.com/day-16-word-counter-in-python/