0
Please identify the issue
text = input() words = len(text.split()) letters = len(text) average = letters / words print(average)
6 Antworten
+ 3
letters = len(text) counts all characters in the input text, including spaces. That's why you get 6.6666666666 for your example
+ 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!
+ 2
words = text.split()
letters = sum(len(w) for w in words)
avg = letters/len(words)
print(avg)
+ 1
Input: sololearn is awesome
Output: 6.666666666666
Expected Output: 6.0
+ 1
0
This is an exercise while doing python data structure which is not getting done