0

Please identify the issue

text = input() words = len(text.split()) letters = len(text) average = letters / words print(average)

16th Mar 2021, 6:37 PM
Nasir Niamat
Nasir Niamat - avatar
6 Answers
+ 3
letters = len(text) counts all characters in the input text, including spaces. That's why you get 6.6666666666 for your example
16th Mar 2021, 7:03 PM
Benjamin JĂźrgens
Benjamin JĂźrgens - avatar
+ 3
Nasir Niamat , the issue in your code is that it does not only count the characters of the words, it also counts the spaces inbetween. try to rework your code by doing it step by step ▪︎split input string to a list by using " " as a separator. ▪︎iterate through this list with a for loop and use a variable total that can hold the sum of all characters ▪︎in each iteration of the loop you get one word. get the length of this word and add this value to the var total ▪︎after finishing the loop you have the total number of characters counted in var total ▪︎use this var and divide it by the number of words ( or number of elements of the list) that's it. happy coding!
16th Mar 2021, 7:06 PM
Lothar
Lothar - avatar
+ 2
words = text.split() letters = sum(len(w) for w in words) avg = letters/len(words) print(avg)
16th Mar 2021, 6:47 PM
visph
visph - avatar
+ 1
Input: sololearn is awesome Output: 6.666666666666 Expected Output: 6.0
16th Mar 2021, 6:44 PM
Nasir Niamat
Nasir Niamat - avatar
+ 1
Yes, the issue was spaces. Thank you Lothar, Benjamin JĂźrgens, visph, Josh Greig
16th Mar 2021, 7:11 PM
Nasir Niamat
Nasir Niamat - avatar
0
This is an exercise while doing python data structure which is not getting done
16th Mar 2021, 6:45 PM
Nasir Niamat
Nasir Niamat - avatar