2

Hello guys I am still an armature in python was hoping if anyone could help with this solution.

Write a function called longest which will take a string of space separated words and will return the longest one. For example:

longest("This is Fabulous") => "Fabulous" longest("F") => "F"

class Test(unittest.TestCase):
 def test_longest_word(self):
        sentence = "This is Fabulous"
        self.assertEqual('Fabulous', longest(sentence))

 def test_one_word(self):
        sentence = "This"
        self.assertEqual("This", longest(sentence))

This is my solution so far;

def find_longest_word(word_list):  
    longest_word = ''  
    longest_size = 0   
for word in word_list:    
    if (len(word) > longest_size)
    longest_word = word
    longest_size = len(word)      
return longest_word

words = input('Please enter a few words')  
word_list = words.split()  
find_longest_word(word_list) 

Unfortunately am getting this error when I try to test the code "File "", line 6 if (len(word) > longest_size) ^ SyntaxError: invalid syntax

Any help please I will highly appreciate?

HeKe
  • 41
  • 2
  • 9

4 Answers4

1
def find_longest_word(myText):
  a = myText.split(' ')
  return max(a, key=len)


text = "This is Fabulous"
print (find_longest_word(text)) #Fabulous

EDIT: The solution above works if you want one of the longest words and not all of them. For example if my text is "Hey ! How are you ?" It will return just "Hey". If you want it to return ["Hey", "How", "are", "you"] Better use this.

def find_longest_word(myText):
  a = myText.split(' ')
  m = max(map(len,a))
  return [x for x in a if len(x) == m]

print (find_longest_word("Hey ! How are you ?"))  #['Hey', 'How', 'are', 'you']

See also, this question

Espoir Murhabazi
  • 5,973
  • 5
  • 42
  • 73
Elmehdi93
  • 70
  • 1
  • 9
  • Am getting this error Traceback: in NameError: name 'Fabulous' is not defined – HeKe Jun 20 '17 at 11:30
  • Delete the "// Fabulous" and the " // ['Hey', 'How', 'are', 'you']" I was just telling you what it's supposed to print – Elmehdi93 Jun 20 '17 at 11:43
  • Need to be careful with .split(" ") as this will not split all spaces. It will not split "James/nBlunt" for example. Better to leave .split() blank if you are splitting a text with multiple lines. – Patrick Moloney Mar 22 '21 at 19:51
0

You are missing the : at the end of the if statement

Use the updated code below, I fixed your indentation issues too.

def find_longest_word(word_list):  
    longest_word = ''  
    longest_size = 0   
    for word in word_list:    
        if (len(word) > longest_size):
            longest_word = word
            longest_size = len(word)      
    return longest_word

 words = input('Please enter a few words')  
 word_list = words.split()  
 find_longest_word(word_list) 
Colwin
  • 2,655
  • 3
  • 25
  • 25
0

Code sample is incorrect. I get the following message if I try to output:

Error on line 15: print(longest_word("chair", "couch", "table"))

TypeError: longest_word() takes 1 positional argument but 3 were given

So the code looks like this:

def longest_word(word_list):  
    longest_word = ''  
    longest_size = 0   
    for word in word_list:    
        if (len(word) > longest_size):
            longest_word = word
            longest_size = len(word)      
    return longest_word
    words = input("chair", "couch", "table")  
    word_list = words.split()  
    find_longest_word(word_list) 
Community
  • 1
  • 1
susanf
  • 11
  • 1
  • 1
  • 2
0
# longest word in a text
text = input("Enter your text")
#Create a list of strings by splitting the original string
split_txt = text.split(" ")
# create a dictionary as word:len(word)
text_dic = {i:len(i)for i in split_txt}
long_word = max([v for v in text_dic.values()])
for k,v in text_dic.items():
    if long_word == v:
        print(k)