+ 2
Average word length code coach python
Only 2 hidden test cases fail. Here’s my code: sentence = str(input()).split() i = 0 for x in sentence: i += len(x) print(round(i / len(sentence)))
12 Answers
+ 2
You are missing
* Remove all punctuation.
* Round up to the nearest whole number.
Punctuation like *, #, @,!, $, %, &,?,., , .. Should be removed from text then find length of text..
edit:
Nikola Markov
s is list, not a string. So
if '?' In s : fails for "ab?cd" . Only pass "?"
You need a loop to go through list then remove list items punctuation if any. Store back result.
Next find average..
+ 6
Nikola Markov ,
(referring to your last posted code)
the task description says:
> Task:
Takes in a string, figure out the average length of all the words and return a number representing the average length. *Remove all punctuation*.
Round up to the nearest whole number.
> so the part for *removing all punctuation* is missing.
+ 3
Try ceil instead of round.
+ 3
Is there actually asked to apply round or round up explicitly in description?
If not , then dont apply any.. Just put
print( i/len(sentence) )
+ 2
Now the first case fails
Here’s the code:
import math
sentence = str(input()).split()
i = 0
for x in sentence:
i += len(x)
print(math.ceil(i / len(sentence)))
+ 2
For example: answer is ceil(26/5)
So apply ceil function. If not work, can you add full description or link.. There are multiple questions about task and multiple python courses. One can i see is just asks avg of 26/5
+ 2
I did it. Thank you to everyone who answered my question.
Here’s the code:
import math
def listToString(s):
string = ""
for element in s:
string += element
return string
s = input()
b = s.split()
lst = []
i = 0
for letter in s:
if letter != '?' and letter != '.' and letter != ' ' and letter != '!':
lst.append(letter)
a = listToString(lst)
print(math.ceil(len(a) / len(b)))
+ 1
It could also be a division by zero.
And no need str(input()), input's function return string by default
0
The first case printed 3 before, but now it prints 4. If i dont use round 5,20 returns as 5 and not 6, so we have to use round or ceil and if i use // it returns as 5. Also sorry for responding so late it was late for me and sorry for any mistakes i make i am a beginner and english isnt my first language.
The 5,20 and 6 came from the example where it says
Sample Input:
this phrase has multiple words
Sample output:
6
0
I think i know why it happens but not how to fix it. In the first case the last word is that but theres a ? and it counts it as part of the word.
0
You are in a college level English class, your professor wants you to write an essay, but you need to find out the average length of all the words you use. It is up to you to figure out how long each word is and to average it out.
Task:
Takes in a string, figure out the average length of all the words and return a number representing the average length. Remove all punctuation. Round up to the nearest whole number.
Input Format:
A string containing multiple words.
Output Format:
A number representing the average length of each word, rounded up to the nearest whole number.
Sample Input:
this phrase has multiple words
Sample Output:
6
Explanation:
The string in question has five words with a total of 26 letters (spaces do not count). The average word length is 5.20 letters, rounding it up to the nearest whole numbers results in 6.
0
My code that fails:
import math
s = input().split()
if '?' in s:
s.remove('?')
elif ',' in s:
s.remove(',')
elif '.' in s:
s.remove('.')
elif '!' in s:
s.remove('!')
i = 0
for x in s:
i += len(x)
print(math.ceil(i / len(s)))