Write a short code segment that prints the longest word is a list of word w3c schoop
Answers
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()
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()