Create a program, longest.py, that has a function that takes one string argument and prints a sentence indicating the longest word in that string. If there is more than one word print only the first. Your print statement should read: “The longest word is x” Where x = the longest word. The word should be all lowercase. I have the correct code, but the out put should only print: "The longest word is x". In my case it asks me an input first which I don't need. string = input('Enter string:'). My question is to remove the 'Enter string:' from my code, so that I get the correct working prints with only: "The longest word is x". I tried to remove that, but could print. Thanks

EBK JAVA PROGRAMMING
9th Edition
ISBN:9781337671385
Author:FARRELL
Publisher:FARRELL
Chapter7: Characters, Strings, And The Stringbuilder
Section: Chapter Questions
Problem 12RQ
icon
Related questions
Question

Q: Create a program, longest.py, that has a function that takes one string argument and prints a sentence indicating the longest word in that string. If there is more than one word print only the first. Your print statement should read: “The longest word is x” Where x = the longest word. The word should be all lowercase.

I have the correct code, but the out put should only print: "The longest word is x". In my case it asks me an input first which I don't need. string = input('Enter string:'). My question is to remove the 'Enter string:' from my code, so that I get the correct working prints with only: "The longest word is x". I tried to remove that, but could print. Thanks 

------------------------------------------------------------------------------



def longestWord():
    length = 0 
    x: longestWord() = '' 

for word in string.split():
   if len(word) > length:
   length = len(word)
   x = word
return x

*****string = input('Enter string:')  **** (want to remove this part)


   x = longestWord()
   print('The longest word is', x)   # should only print with this

 

longestWord()

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
EBK JAVA PROGRAMMING
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT