+ 5

Problem: Average Word Length

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 My code that works: sentence = input() words = sentence.split() total = sum(map(len, words))/len(words) print(total)

22nd Feb 2021, 1:38 PM
Max_Mnemo
Max_Mnemo - avatar
54 Answers
+ 19
text = input() length_words = 0 total_words = 0 words = text.split( ) for x in words: length_words += len(x) total_words += 1 average = length_words / total_words print(average)
22nd May 2021, 6:11 PM
HARSHAVARDHAN REDDY
HARSHAVARDHAN REDDY - avatar
+ 8
words = input() len_words= len(words) - words.count(" ") tot = len_words / (words.count(" ")+1) print(tot)
28th Aug 2021, 9:30 PM
Hamdambek🖥️🎧
Hamdambek🖥️🎧 - avatar
+ 3
Here are go. Did this in Python sentence = input("") words = sentence.split() count = 0 sum =0 for x in words: sum += len(x) count += 1 total = sum / count print(total)
21st Apr 2021, 11:33 AM
Joe Hutchens
Joe Hutchens - avatar
+ 3
txt = input("") length = len(txt)-txt.count(" ") splits = txt.split() words = 0 for splits[words] in splits: words += 1 print(length/words)
16th Aug 2021, 3:47 PM
Thamon Muengma
+ 3
New approach without using split and join (). s=str(input()) print((len(s)-s.count(' '))/(s.count(' ')+1))
29th Nov 2021, 3:42 PM
Sonali Kumari
Sonali Kumari - avatar
+ 2
0 text = input() a = text.split() b=len(a) only_alpha = "" for i in text: if i.isalpha(): only_alpha += i print(len(only_alpha)/b) its work try you
22nd Apr 2021, 1:00 PM
Budhabhushan Waghmare
Budhabhushan Waghmare - avatar
+ 2
text = input() new_list=text.split() text = text.replace(" ","") print(len(text)/len(new_list))
1st Oct 2021, 8:06 AM
jiaqi chen
jiaqi chen - avatar
+ 2
new way to do it without using split() good for interview def average(text): space=" " word=[] i=0 new_text=text.replace(" ","") length_of_text=len(new_text) while i < len(text): if text[i] not in space: word_start = i while i
2nd Oct 2021, 6:56 AM
jiaqi chen
jiaqi chen - avatar
+ 2
text = input() length_words = 0 total_words = 0 words = text.split( ) for x in words: length_words += len(x) total_words += 1 average = length_words / total_words print(average)
4th Jan 2022, 6:22 AM
prateek tiwari
prateek tiwari - avatar
+ 2
nsenji.inc add a space to 'punc' list. Your 2nd for loop is no need.
11th Apr 2022, 8:51 PM
Jayakrishna🇮🇳
Jayakrishna🇮🇳 - avatar
+ 1
Can anyone help me wit it
14th Apr 2021, 12:44 PM
Kaiz arafat
Kaiz arafat - avatar
+ 1
# Super simple!!! text = input() length_words = 0 total_words = 0 words = text.split( ) for x in words: length_words += len(x) total_words += 1 average = length_words / total_words print(average)
3rd Jun 2021, 6:06 PM
LAKSHMI NARAYANAN M
LAKSHMI NARAYANAN M - avatar
+ 1
Here is my code solution: text = input() WORDS = text.split(" ") count = 0 for i in text: if not i == " ": count += 1 average = count/len(WORDS ) print (average )
24th Jul 2021, 7:51 PM
Mwaniki Grace Waigumo
Mwaniki Grace Waigumo - avatar
+ 1
text = input() length = len(text) - text.count(' ') count = len(text.split()) print(length/count)
31st Jul 2021, 11:32 AM
David labadze
David labadze - avatar
+ 1
Here's a new approach and keeping it a 2 line code text = input().split() print(len("".join(text)) / len(text))
12th Sep 2021, 12:41 AM
TrebleClef20
TrebleClef20 - avatar
+ 1
text = input() result = len(text.replace(' ', '')) / len(text.split()) print (result)
23rd Oct 2021, 7:08 PM
Morteza Abdollahi
Morteza Abdollahi - avatar
+ 1
check this out here: text = input() text_list=list(text.split()) text_length=len(text_list) letter=list(text) letter_length=len(letter) -(text_length-1) average=letter_length/text_length print(average)
7th Dec 2021, 3:50 AM
Md. Almas Uddin Riad
Md. Almas Uddin Riad  - avatar
+ 1
text = input() a = text.split() d = 0 for i in a: c = i.split() e = len(i) d += e print(d/len(a))
3rd Jan 2022, 4:08 PM
Suyash Kadam
+ 1
import numpy as np n = str(input()) b = len(n.split()) def func(a, b, c=0): for i in a: if i != " " and i !="?": c += 1 print (int(np.ceil(c/b))) func(n, b)
28th Jan 2022, 10:27 PM
Róbert Erdő
Róbert Erdő - avatar
+ 1
When I tried it like this....the first case has refused but the other 4 cases are ryt...what could be wrong Mohamed Amr [user id="19448304"]Pratap Vasava[/user] [user id="18715899"]LAKSHMI NARAYANAN M[/user] story = input() i = 0 punc = ["?","!",".",","] for word in story: for char in word: if char in punc: word = word.replace(char,"") else: u = len(word) i=i+u new_story = story.split() answer = i/(len(new_story)) print(round(answer))
9th Apr 2022, 4:37 PM
nsenji.inc