Computer Science, asked by PriyaVaghela4208, 1 year ago

Write a short code segment that prints the longest word is a list of word w3c schoop

Answers

Answered by BrainlyTech
7

\textbf{ Here is a short code }

def main():

   filename = input("The filename?")

   with open(filename) as f:

       linenum = 1

       for line in f:

           words = line.split()

           longest = ''

           for word in words:

               if len(longest) < len(word):

                   longest = word

           print("Line", linenum, "has", longest, "as the longest word.")

           linenum += 1

           print(longest)

main()

Answered by TalentedLady
4

\LARGE\sf {\underline{\underline\green{Required \:  Answer:-}}}

def main():

filename = input("The filename?")

with open(filename) as f:

linenum = 1

for line in f:

words = line.split()

longest = ''

for word in words:

if len(longest) < len(word):

longest = word

print("Line", linenum, "has", longest, "as the longest word.")

linenum += 1

print(longest)

main()

Similar questions