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!
9 Antworten
+ 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.
+ 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
+ 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.
+ 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.
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*.
0
Yes I know but i'm struggling to not to make the variable words_sum count the spaces.
0
Thank you very much!
0
XXX Oh, I see.
- 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