Question
Write a function called average_word_length that takes as #input a string called my_string, and returns as output the #average length of the words in the
Write a function called average_word_length that takes as #input a string called my_string, and returns as output the #average length of the words in the string. # #In writing this function, note the following: # # - You should account for consecutive spaces. A string like # "Hi Lucy" is two words with an average length of 3.0 # characters. # - You should not assume the string starts with a letter. # A string like " David" has one word with an average # length of 5.0 characters. # - You should not count punctuation marks toward the # length of a word. A string like "Hi, Lucy" has two # words with an average length of 3.0 characters: the , # after "Hi" does not count as a character in the word. # The only punctuation marks you need to handle are # these: . , ! ? # - You may assume the only characters in the string are # letters, the punctuation marks listed above, and spaces. # - If my_string is not a string, you should instead return # the string, "Not a string". # - If there are no words in my_string, you should instead # return the string, "No words". This could happen for # strings like "" (an empty string) and ".,!?" (a string # of only punctuation marks). You may assume that any # of these punctuation marks will always be followed by # at least one space. # #Here are a few hints that might help you: # # - You can peak at the first character in my_string with # my_string[0]. If my_string is "Hi, Lucy", then the value # of my_string[0] is "H". You don't have to do this, but # you can if you want. # - There are lots of ways you can do this. If you're # stuck, try taking a step back and thinking about the # problem from a fresh perspective. # - If you're still stuck, try counting words and letters # separately, and worrying about average length only # after both have been counted. # - The word count should equal the number of letters that # come immediately after a space or the start of the # string. The character count should simply equal the # number of characters besides spaces and punctuation # marks. The average word length should be character # count divided by word count.
#When your function works, the following code should #output: #2.0 #3.0 #4.0 #Not a string #No words
Given (bottom) Lines:
print(average_word_length("Hi")) print(average_word_length("Hi, Lucy")) print(average_word_length(" What big spaces you have!")) print(average_word_length(True)) print(average_word_length("?!?!?! ... !"))
Thanks so much..will give a thumbs up for effort (right or wrong)..Btw please use try and except keywords if possible
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started