Skip to content

Commit 6c5c7e7

Browse files
Program 17
1 parent cbb083e commit 6c5c7e7

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

Lab Cycle 2/program17.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# Program to accept a list of words and return the length of the longest word.
22

3+
# Function to find the ength of the longest word
34
def longest_word(words):
4-
longest = 0
5-
for word in words:
6-
if len(word) > longest:
7-
longest = len(word)
8-
longest_word = word
9-
print("Longest word is",longest_word)
10-
print("Length of longest word is",longest)
5+
longest = 0 # Initialize the longest word length to 0
6+
for word in words:
7+
if len(word) > longest: # If the length of the current word is greater than the longest word length
8+
longest = len(word) # Set the longest word length to the length of the current word
9+
longest_word = word # Set the longest word to the current word
10+
print("Longest word is",longest_word) # Print the longest word
11+
print("Length of longest word is",longest) # Print the length of the longest word
1112

12-
print("Enter a list of words separated by spaces: ")
13-
words = input().split()
14-
longest_word(words)
13+
print("Enter a list of words separated by spaces: ")
14+
words = input().split() # Get the list of words from the user
15+
longest_word(words) # Function call

0 commit comments

Comments
 (0)