Saturday, May 31, 2014

Word Count Program in Python


This simple program that I wrote in Python programming language will ask the user to enter a sentence and then the program will count the number of words in the given sentence. I called this program Word Count Program in Python.The first part of program it will ask the user to enter a sentence using this command sentence = input("Please enter a sentence : ") the variable sentence will act as a holder in a given sentence by user. In Python the keyword input will accept a word or sentence from the user.

The second part of the program is the counting of words in the given sentence by the user I am using a variable count_words. After which I use the for loop statement to split the words in the given sentence of the user and then the variable counter to find the length of the sentence stored in variable a. Lastly the statement 
count_words += counter to count the number of words in the given sentence.

Finally after we accept input sentence from the user, count the number of words in the given sentence and then display the result of the screen. Making programs in Python is much easier compared to other programming languages because it has many built in functions to manipulate data such as numbers and strings.

I hope you will find my work useful in your quest in learning for to program in Python programming language.
Learning how to program is a journey and needs constant practice, perseverance and creative thinking.

Thank you very much.


Sample Out of Our Program


Python Text Editor Environment

Program Listing

# words.py
# Written By: Mr. Jake R. Pomperada, MAED-IT
# Language : Python
# Date     : May 31, 2014

def main():

    print("==========================")
    print("   Word Count Program     ")
    print("==========================")
    print("\n")

    sentence = input("Please enter a sentence : ")

    count_words = 0

    for a in sentence.split():
        counter = len(a) / len(a)
        count_words += counter
        
    print("\n")    
    print("Total number of words in a given sentence" ,int(count_words),".")
    print("\n")    
    print("\t Thank You For Using This Program")
    print("\n")    

main()






No comments:

Post a Comment