简体   繁体   中英

Write a function that takes a list of words and returns the longest word and length of the longest one

The full question is "Write a function find_longest_word() that takes a list of words and returns the longest word and length of the longest one. You should also write a test function that will request the users for a list of words and print the longest word and its length."

I wrote this code and it works. I just need to figure out how i can add a function into this code to to what I am told in the question.

def main():
    text = input("Please input a list of words to evaluate: ")

    longest = 0

    for words in text.split():
        if len(words) > longest:
            longest = len(words)
            longest_word = words

    print("The longest word is", longest_word, "with length", len(longest_word))

main() 

Simply move the logic, into a function, like this

def find_longest_word(text):
    for words in text.split():
        if len(words) > longest:
            longest = len(words)
            longest_word = words
    return longest_word, len(longest_word)

def main():
    input_string = input("Please input a list of words to evaluate: ")
    longest_word = find_longest_word(input_string)
    print("The longest word is", longest_word, "with length", len(longest_word))

The actual problem, you are trying to solve can be solved like this

def find_longest_word(text):
    longest_word = max(text.split(), key = len)
    return longest_word, len(longest_word)

Note: Instead of printing the result in the find_longest_word function, we are returning the longest_word from it, so that the function does only what it is supposed to do. Now, as the name suggests, it just finds the longest word.

This exercise with a short codec....

def main():
    a = sorted(input("Please input a list of words to evaluate: ").split(), key=len) [-1]
    print ("You longe word ", str(a), " have this", str(len(a)), "lenghts")

main()  

You do not need longest_word = words , but without your answer, It was not possible to answer this exercise ...., so thanks!

def main ():
    text = input("Please input a List of words to evaluate: ")

    longest = 0

    for words in text.split():
           if len(words) > longest:
                  longest = len(words)
    print ("The longest word is", words, "with lenght", longest)


main()
def find_longest_word(words_list):  
    word_len = []  
    for n in words_list:  
        word_len.append((len(n), n))  
    word_len.sort()  
    return word_len[-1][1]  

print(find_longest_word(["hasta ", "lavista", "baby"])) 

this is a python program to read a list of words and return the length of the longest one:

list_words = ["PHP", "Exercises", "Backend"]<br/>
s = [(len(word),word) for word in list_words] <br/>
s.sort() <br/>
print('Longest String in list is ',s[-1][1]) <br/>
numberOfWordsInList = int(input("Enter number of words to be List: "))

def longestWordInList(numberOfWordsInListt):
    createdList = [input("Enter any word: ") for i in range(numberOfWordsInList)]    
    lengthOfItems = [(len(items), items) for items in createdList]
    return max(lengthOfItems)

print(longestWordInList(numberOfWordsInList))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM