Write a Python Function That Takes a List of Words And Returns The Length of The Longest One

Write a Python Function That Takes a List of Words And Returns The Length of The Longest One

Hi Guys, welcome to my blog, lets try to get python solution for how to write a python function that takes a list of words and returns the length of the longest one



Title :

Python Problem : write a python function that takes a list of words and returns the length of the longest one

Write a Python Function That Takes a List of Words And Returns The Length of The Longest One




Description :

Take a function and pass the input as a list of words, inside the function initialize the longest variable to zero so this variable will be used to store the length of the longest word present in the list of words. 

Using a for loop, iterate all the world's present in the list of words, check if the length of the word is greater than the longest then initialize the longest to the length of the word. 

Once the for loop ends, return the length of the longest word that we have computed above.

def get_longest_length(words):
   longest = 0
   for word in words:
       if len(word) > longest:
           longest = len(word)
   return longest

 

 

In the main program take a length variable and ask the user to enter the number of words using input function call and then convert it into an integer type.

Now take an empty list and ask a user to enter the list of words by iterating from zero to the list length. This is the input function call inside the for loop, 

Once we keep getting the words appointed to the list of words.

length = int(input("Enter Number of Words : "))
word_list = []
for _ in range(length):
   word = input("Enter a Word : ")
   word_list.append(word)

 

 

Take another variable name max_length to store the Max length of word by calling the above defined function. Once the function gets executed, use the print function call to print the max_length.

max_length = get_longest_length(word_list)
print("Longest Length is : ", max_length)



Code :


def get_longest_length(words):
    longest = 0
    for word in words:
        if len(word) > longest:
            longest = len(word)
    return longest


length = int(input("Enter Number of Words : "))
word_list = []
for _ in range(length):
    word = input("Enter a Word : ")
    word_list.append(word)

max_length = get_longest_length(word_list)
print("Longest Length is : ", max_length)




Output :

Enter Number of Words : 4
Enter a Word : hello
Enter a Word : code
Enter a Word : with
Enter a Word : tj
Longest Length is :  5



Conclusion :

The above Python program uses the Python function to calculate the maximum length of a word in a given list of words. The program also accepts the input words from the user and evaluates it. Execute the above Python program by providing the random words as input to it and note down the results. Comment it down below if you have any suggestions to improve the above on program

Post a Comment

0 Comments