- 1
help please
Im doing python's data structures and im in the 2nd project wich i have to calculate the average word length. And i need to divide the sum of all word lengths by the number of words in the sentence. example taked it by the project: 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 and my code takes the spaces as a character and i dont know how to not do it,i used strip() but it didnt work and im gonna try a different way: texto = input() lista=texto.split() resultado=texto/len(lista) print(resultado)
7 Respostas
+ 4
That's great
0
resultado should hold sum of length of each word divided by number of words (mean of word length): len(lista) give you the number of words, but texto is the input text...
you should iterate lista and add each word length, then divide the result by len(lista)
resultado = 0
for word in lista:
resultado += len(word)
resultado /= len(lista)
0
Thank you very much for your help ill try the code
0
Thank you very much visph for yout help
0
text = input()
L_text=text.split()
cont=0
for palabra in L_text:
for letra in palabra:
cont+=1
print(cont/len(L_text))
0
text = input()
L_text=text.split()
count=0
for palabra in L_text:
for letra in palabra:
count+=1
print(count/len(L_text))
- 1
a="hey world hello"
b=a.split()
print(sum(list(len(i) for i in b))/len(b))
Or
a="hey world hello"
b=a.split()
add=0
for i in b:
add+=len(i)
print(add/len(b))