+ 1

Kindly help

Q. Given a sentence as input, calculate and output the average word length of that sentence. To calculate the average word length, you need to divide the sum of all word lengths by the number of words in the sentence. Sample Input: this is some text Sample Output: 3.5 Explanation: There are 4 words in the given input, with a total of 14 letters, so the average length will be: 14/4 = 3.5 hello everyone, for the above question, i am trying this syntax to get the ans. but the output I'm getting is 6.666666666666667 and the expected output is 6.0 kindly help me to understand the and to get the right ans. thank you. text = input() a = text.split() print (len(text)/len(a))

12th May 2022, 1:36 PM
Rajat Singh
3 Answers
+ 3
Please DO NOT put links in the tag section: * specify the relevant programming language in the tag section * put links in the description.
12th May 2022, 2:58 PM
Lisa
Lisa - avatar
+ 2
Rajat Singh , a common way to solve the task is: - take input and split it at word boundary (spaces), this creates a list of words - iterate through the list of words, get the length of each word and build the sum of them. this is the total numer of characters without spaces - take the sum of word length and divide it by the numer of words. we can use len() to get the number of words i am not sure if the result has to be rounded in a defined way, so please refer to the task description
12th May 2022, 2:53 PM
Lothar
Lothar - avatar
+ 1
Rajat Singh len(text) includes spaces also. So you need to subtract space count from length
12th May 2022, 1:57 PM
Jayakrishna šŸ‡®šŸ‡³