Skip to content

Instantly share code, notes, and snippets.

@brainyfarm
Created August 8, 2016 06:54
Show Gist options
  • Save brainyfarm/da6389dabdf70c60d6e118fe1fe68c1c to your computer and use it in GitHub Desktop.
Save brainyfarm/da6389dabdf70c60d6e118fe1fe68c1c to your computer and use it in GitHub Desktop.
FreeCodeCamp Return length of longest word in sentence (Python)
"""
Return the length of the longest word in the provided sentence.
Your response should be a number.
"""
def longest_word_length(sentence):
# Length of longest word before looping
longest_length = 1
# Split the sentence into a list of words
word_list = sentence.split(" ")
# Loop through the list of word
# Check the length of current word and assign it as the longest length
# if it is greater than the value of longest_length
for current_word in word_list:
if len(current_word) > longest_length:
longest_length = len(current_word)
# Return longest_length value at the end of loop
return longest_length
print longest_word_length("I am a very great guy and I know it!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment