Aug-15-2018, 02:36 PM
Hi everyone,
I'm looking for some help with an exercise I was doing, regarding strings in python.
This program is supposed to find the longest word out of a list of words the user types (i.e. if I type "chicken, dog and cat" the output will be "chicken"), but for the sake of a possibility, if I did type, for example, only "dog and cat", the output will always be dog. Thing is, I want my program to print them both.
Here is the code:
)
I'm looking for some help with an exercise I was doing, regarding strings in python.
This program is supposed to find the longest word out of a list of words the user types (i.e. if I type "chicken, dog and cat" the output will be "chicken"), but for the sake of a possibility, if I did type, for example, only "dog and cat", the output will always be dog. Thing is, I want my program to print them both.
Here is the code:
def longest_word(l):
lenl=[]
for n in l:
lenl.append((len(n), n))
lenl.sort()
return(lenl[-1][1])
lis=input("Insert some words ").replace(",", " ").replace("and", " ")
lispar=lis.split(" ")
print(longest_word(lispar))Thanks in advance! ( Oh, and if you find anything I could improve here, furthermore, just let me know, it's very appreciated
)
