python35 Define a function getWordLengthAverage that has 1 p
python3.5
Define a function getWordLengthAverage that has 1 parameter of type string. The string can be empty or can contain a sentence. The function calculates the average length of the words that are in the sentence and returns the average. You can assume the parameter is always a string, and for the simplicity, the words are separated by a space, and there are no double spaces. Example: The function should return 4.142857 for “James, do you want to play outside?”. Please note ‘James,’ including the ‘,’ and ‘outside?’ including the ‘?’ are both considered a word for this problem, since we are assuming that words are separated only by spaces. Using assertEqual, verify your function with at least 3 test cases.
Solution
this is the fallowing code for getting the average length of a string: >>> sntnce = \"James, do you want to play outside?\" >>> words = sntnce.split() >>> avg = sum(len(word) for word in words)/len(words) >>> avg 4.142857142857143