+ 1

Please can you tell me why it doesn't work ?

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 There is the code : text = input() res=text.split() for i in res : total=len(i) sum=len(res) leng=total/sum print(leng)

10th Feb 2022, 11:44 AM
Programer F_K
Programer F_K - avatar
5 Answers
+ 5
Programer F_K , we need to modify your code slightly: text = input() res=text.split() total = 0 # <<< we need to initialize the variable that gets the total number of characters for i in res : total+=len(i) # <<< we need to use an other assignment operator to get the total number of characters sum=len(res) leng=total/sum print(leng)
10th Feb 2022, 11:56 AM
Lothar
Lothar - avatar
+ 1
ravilnicki Okey ,I'll try this . Thank you .
10th Feb 2022, 11:56 AM
Programer F_K
Programer F_K - avatar
+ 1
Lothar It worked ,thanks .
10th Feb 2022, 12:00 PM
Programer F_K
Programer F_K - avatar