DEV Community

Cover image for Word Counter
Scott Gordon
Scott Gordon

Posted on

Word Counter

# word_counter.py
#   This program counts the number of words in a sentence entered
#   by the user.
# by: Scott Gordon

def main():

    # Print greeting message
    print("***** Welcome to Word Counter *****")

    # Input users sentence and assign it to a variable.
    text = input("Enter your sentence here: ")

    # Split the sentence and save it to a list.
    split_text = text.split()

    # Count the number of words in the list.
    word_count = 0
    for word in split_text:
        word_count += 1

    # Print word count to the screen.
    print(f"The word count for your sentence is: {word_count}")


main()
Enter fullscreen mode Exit fullscreen mode

Photo by Finn Mund on Unsplash

Top comments (0)