+ 2

# Average word length

Given a sentence as input, calculate and output the average word length of that 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 I tried this, and n1 and n2 are correct, but n1/n2 turns out to be wrong. text = input() n1 = len(text) n2 = len(text.split()) print((n1/n2))

10th Oct 2021, 2:02 PM
Jieyu Wang
+ 10
As Indira and Simon pointed out, n1 is also counting space. So we can remove unnessary space with replace () text = input() n1 = len(text.replace(" ","")) n2 = len(text.split()) print((n1/n2))
10th Oct 2021, 2:15 PM
Myo Thuzar
+ 6
Indira , i thought that you are counting spaces. hmmm.... anyway: what i have seen is that if the input for your code is "yes" , the result shown is 4, which is not correct. using input "yes yes yes", n1 will show 10 letters instead of 9. the reason is that your counter 'n1' is initialized with 1 instead of 0.
11th Oct 2021, 3:48 PM
Lothar
+ 4
U are counting space too here... U are supposed to count letters only in n1.. Do this.. Hope it helps text = input() n1=0 for i in text: if(i!=' '): n1+=1 n2 = len(text.split()) print((n1/n2))
10th Oct 2021, 2:12 PM
Indira
+ 3
Jieyu Wang , to get the average word length, we need to know how many words the sentence has. we can split the input at the word boundary by using .split(). this will result in a list. the length (number of elements or words) of the list is the number of words. the next step is to calculate the total number of characters. we can use the join() function with the list and create a string from the elements in the list. join() has to be used with an "empty" string. the result of this operation is a string that contains only characters but no spaces. the length of the string is the number of characters. finally you can use the number of words and the number of total characters to get the result. please be aware, that there might be a kind of rounding is required by the task description.
10th Oct 2021, 2:14 PM
Lothar
+ 3
Indira , there might be a risk by counting the spaces (as you have done) and assume that they give you the correct number of words. this will work as long as there are no multiple spaces between the words and as long as there are no leading or trailing white spaces. if we have these accidently issues the calculation will not be correct.
10th Oct 2021, 2:39 PM
Lothar
+ 2
In n1 you're counting spaces as letters.
10th Oct 2021, 2:05 PM
Simon Sauter
+ 2
Lothar I'm not counting spaces there.. it is if (i!=' ' ) means if i is not equal to space then count+1..Otherwise ignore.. It's only counting letters..
11th Oct 2021, 3:12 AM
Indira
+ 2
My solution text = input().split() letters=0 words=0 for i in text: letters+=len(i) words+=1 print(letters/words)
2nd Oct 2022, 12:09 PM
Arevik Khachatryan
+ 1
Thank you everyone, I tried the replace method and it works!
10th Oct 2021, 2:22 PM
Jieyu Wang
+ 1
Lothar Yeah.. n1 will be 0..
12th Oct 2021, 12:46 AM
Indira
0
sum(len(i) for i in text.split()) / len(text.split())
10th Oct 2021, 3:36 PM
ubai
0
I just used stored all my numbers in variables for later use. text = input() # use split() function to obtain a list of all words # use len() function to get length of each words # calculate the average by taking the total length / amount of words # use for loop over the individual words textWords = text.split() letters = [len(i) for i in textWords] count = 0 for num in letters: count += num print(count / len(letters))
16th Jun 2022, 8:15 PM
Devan Hall