Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
- 1

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)

12th Mar 2021, 7:52 PM
Yami Francø
7 Antworten
+ 4
That's great
28th Apr 2021, 4:25 AM
Sash The Smartest Alien
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)
12th Mar 2021, 8:06 PM
visph
0
Thank you very much for your help ill try the code
12th Mar 2021, 8:07 PM
Yami Francø
0
Thank you very much visph for yout help
12th Mar 2021, 8:35 PM
Yami Francø
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))
28th Feb 2022, 9:29 PM
Liria
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))
11th Oct 2022, 5:37 PM
prabhanshu jha
- 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))
12th Mar 2021, 8:00 PM
Abhay