+ 2
Is there a better way of doing this without using ceil?
Average Word Length 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 https://code.sololearn.com/cNX81Mmdm16k/?ref=app
3 ответов
+ 2
Using math.ceil is perfectly fine. After all, that's what the task required.
+ 1
You should try this one ..
import math
sentence=input()
if sentence .count("?")>0:
length=len(sentence)-1
else:
length=len(sentence )
space=sentence.count(" ")
words=space+1
letters=length-space
average=letters/words
round=math.ceil(average)
print(round)
0
import math
b = input().split()
sum=0
for x in b:
c=len(x)
if x.count("?")>0:
c-= x.count("?")
sum+=c
print(math.ceil(sum/len(b)))