Ask Question

Python3

Write function countLetter that takes a word as a parameter and returns a dcitionary that tells us how many times each alphabet appears in the word (no need to account for absent alphabets). Develop your logic using dictionaries only. Save the count result as a dictionary. Display your output in the main () function.

+5
Answers (1)
  1. 28 October, 03:29
    0
    Following are the program in the Python Programming Language:

    def countLetter (words):

    #set dictionary

    di = {}

    for i in words: #set for loop

    #if key exists in di or not

    if di. get (i):

    #if exists count increased

    di[i] + = 1

    else: #otherwise assign with key

    di[i] = 1

    # return di

    return di

    def main ():

    #get input from the user

    words = input ("Enter the word: ")

    #store the output in function

    letter = countLetter (words)

    #set for loop

    for i in letter:

    print (i, ":", letter[i]) #print output with:

    #call main method

    if __name__ = = "__main__":

    main ()

    Explanation:

    Here, we define the function "countLetter () " and pass the argument "words" in parameter and inside it.

    we set the dictionary data type variable "di". we set the for loop and pass the condition inside it we set if statement and increment in di otherwise di is equal to 1. we return the variable di.

    Then, we define the function "main () " inside it.

    we get input from the user in the variable "words". we store the return value of function "countLetter () " in the variable "letter". we set for loop to print the output.

    Finally, we call the main method.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Python3 Write function countLetter that takes a word as a parameter and returns a dcitionary that tells us how many times each alphabet ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers