+ 2

Can anyone tell me what's wrong with my code. I'm trying the Average Word counter in the code coach but i keep getting 4/5.

import math sentence = input() num = 0 broken_sentence = tuple(sentence.split()) for i in broken_sentence: num += len(i) avg_length = num/len(broken_sentence) print(math.ceil(avg_length))

21st Oct 2020, 5:19 PM
ghali lawal
ghali lawal - avatar
4 Answers
+ 3
ghali lawal , your code is working as expected. Make a use case with 3 words: 9 chars, 6 chars 9 chars. Together this is 24 chars. Divide it by 3 and you will get an average length of 8.
21st Oct 2020, 5:37 PM
Lothar
Lothar - avatar
+ 2
NotAPythonNinja i think Lothar already knew that, but he just wanted to explain the algorithm beside the punctuation problem.👍
21st Oct 2020, 5:52 PM
QTWizard
+ 2
Thank you guys. It was counting punctuation as words. Used the isalpha() to correct that. I really appreciate
21st Oct 2020, 6:05 PM
ghali lawal
ghali lawal - avatar
0
Are you referring to this problem ? https://www.sololearn.com/coach/73?ref=app If yes, then you need to take into consideration if the word contains a punctuation or not like '?,";', which doesn't count into the word length.
21st Oct 2020, 5:33 PM
QTWizard