Need little help! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Need little help!

The problem is : 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. Here is my code and I don't know how to make my variable (words_sum ) to not to count the spaces. text = input() total = text.split() words_in_sentence = len(total) words_sum = len(text) print((words_sum/words_in_sentence) please help!

30th Mar 2021, 12:57 AM
Ailana
Ailana - avatar
9 Answers
+ 1
Ailana A small hint: the number of words in the sentence is 1 more than the number of spaces in the sentence. But you could also do it in another way. Set `words_sum` to 0. Use a for loop to loop over `total` and add the length of each word to `words_sum`. `words_sum` will then have the sum of the length of the words as needed in the problem.
30th Mar 2021, 1:22 AM
XXX
XXX - avatar
+ 1
Calvin Thomas I have a lot of problems with your answer 1. The question never mentioned that the characters "!@#$%....." have to be ignored 2. The question also never mentioned that the answer has to be rounded off. In fact the problem requires the answer to be a float. 3. You gave a direct solution to a code coach. Never give direct solutions to anyone struggling with a code coach (or any other problem). Only give them hints so that they can solve the problem on their own. 4. Even if you did give a direct solution, atleast give one the OP can understand and learn from. The OP was not even able to get the sum of the length of each word, which suggests that they are a beginner. What makes you think that they are aware about methods like str.maketrans() and str.translate()? You didn't even add any comments to explain what is happening on each line. Again, the OP is a beginner, and I don't know about you, but if I were a beginner and saw that code, I would be lost. Happy coding
30th Mar 2021, 7:54 AM
XXX
XXX - avatar
+ 1
XXX Considering the first problem, I had added the extra symbols just to make better use of the maketrans() function. It's true that all the symbols I had added to my code isn't necessary to pass all the tests. Considering the second one, the code-coach problem "Average Word Length" requires the result to be an integer, rounded-off to the largest integer (ceiled). Considering the rest of the issues, I'll make sure that I won't post such direct answers in any Q&A post, unless asked for. I'll also be careful not to use too much of the inbuilt functions while answering a post, unless it requires to. Thanks for the correction.
30th Mar 2021, 8:46 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
Calvin Thomas Thanks for understanding. Regarding the second problem, I just found out that you're talking about the code coach "Average Word Length" in the "Community" section, while I'm talking about the code coach which is the end of module project under "Lists" in the Python Data Structures course. Apparently, they are the same problems with different instructions.
30th Mar 2021, 8:59 AM
XXX
XXX - avatar
0
You have an extra opening-paranthesis '(' on the last line. EDIT: Ailana Also, words_sum is also including the spaces, which it shouldn't as you're counting the length of *each word*.
30th Mar 2021, 1:15 AM
XXX
XXX - avatar
0
Yes I know but i'm struggling to not to make the variable words_sum count the spaces.
30th Mar 2021, 1:16 AM
Ailana
Ailana - avatar
0
Thank you very much!
30th Mar 2021, 1:25 AM
Ailana
Ailana - avatar
0
XXX Oh, I see.
30th Mar 2021, 9:18 AM
Calvin Thomas
Calvin Thomas - avatar
- 1
a = str.maketrans("", "", "!@#$%^&*.,?") a = input().translate(a) a = (len(a)-a.count(" "))/(a.count(" ")+1) print(int(a) + (int(a) < a)) # Hope this helps
30th Mar 2021, 5:59 AM
Calvin Thomas
Calvin Thomas - avatar