+ 2

Average word length

Hello, The question is asking to write 3 functions; word_count, letter_count, and average_word_length. word_count should take as input a string. It should return the number of words in the string. letter_count should take as input a string. It should return the number of letters in the string. average_word_length should take as input a string. It should return the average length of the words in the string. You can find the average length by dividing the number of letters by the number of words. Your implementation for average_word_length *must* call the function word_count and letter_count. Here is the code I have written; def word_count(a_string): words = 0 for letter in a_string: if letter == " ": words += 1 words += 1 return words def letter_count(a_string): letters = 0 for letter in a_string: if not letter == "": letters += 1 return letters def average_word_length(a_string): return (letter_count(a_string)/word_count(a_string)) a_string = "Up with the white and gold" print(average_word_length(a_string)) But according to the reviewer, the answer should be 3.5 and I am getting 4.333333. Please advise where I am going wrong.

1st Sep 2020, 6:50 PM
NG_
NG_ - avatar
12 ответов
+ 5
And one more mistake You have to exclude the punctuation marks too! So do it like this: lst = [" ", ";", "-", ".", ","] if letter not in lst: letters += 1 And, import the math module via the statement: import math And then use the function math.ceil on your answer and then show it
1st Sep 2020, 8:12 PM
Namit Jain
Namit Jain - avatar
+ 5
Nathan Sikati is right! ✅ That's why you should use the split tag So do it like this: def word_count(a_string): return len(a_string.split())
1st Sep 2020, 8:24 PM
Namit Jain
Namit Jain - avatar
+ 4
Also verify that there are non space at the end of the phrase, otherwise, the presence of a space can add a fictive word when counting.
1st Sep 2020, 8:17 PM
Nathan Sikati
Nathan Sikati - avatar
+ 4
Yeah, more efficient and fast.
1st Sep 2020, 8:26 PM
Nathan Sikati
Nathan Sikati - avatar
+ 2
If string contains a space then don't consider it as a charecter in letter_count if not letter == " ":
1st Sep 2020, 7:15 PM
Jayakrishna 🇮🇳
+ 2
If not letter =="": must become If letter !=" ": Take note of the space between the apostroph
1st Sep 2020, 7:50 PM
Nathan Sikati
Nathan Sikati - avatar
+ 2
Thank you so much. I was counting the space also in the string while counting the characters. That was the problem. Again, thanks for your input.
2nd Sep 2020, 11:51 AM
NG_
NG_ - avatar
+ 2
text = input().strip() words = text.split() text = ''.join(words) wlen,tlen = len(words),len(text) print(tlen/wlen)
31st Mar 2021, 7:04 AM
Sachin Rajyaguru
Sachin Rajyaguru - avatar
+ 1
x = input() space_x=x.replace(" ","") len_split_x = len(space_x) y = list(x.split(" ")) len_y=len(y) avg_len = len_split_x/len_y print(avg_len)
28th Dec 2021, 6:28 PM
Ammar Shaikh
Ammar Shaikh - avatar
+ 1
I have solved this problem using this approach text = input() s=text.split() res = sum(not chr.isspace() for chr in text) print(" ",res/len(s))
30th Aug 2022, 8:10 AM
Muhammad Arham
Muhammad Arham - avatar
+ 1
Easy solution text = input().split() letters=0 words=0 for i in text: letters+=len(i) words+=1 print(letters/words)
2nd Oct 2022, 12:06 PM
Arevik Khachatryan
7th Jul 2022, 8:45 AM
Joe Davies
Joe Davies - avatar