+ 3
Trying to solve average word length in python data structure
I can't seem to find what is wrong with my code import re, math string = input() words = float(len(re.sub('\W+',' ',string).split())) letters = float(len(re.sub('\W+','',string))) print(math.ceil(letters/words))
5 Answers
+ 1
Thanks
+ 1
from math import ceil
txt = input().split()
not_char = [".",",","?"]
num_char = 0
for word in txt:
for char in word:
if char in not_char:
continue
num_char += 1
avg = num_char / len(txt)
print (ceil(avg))
0
Thanks
0
Here's the fixed code!
https://code.sololearn.com/ceI3JO3H2DUs/?ref=app
What's gone wrong with your code ?
These two lines :
words = float(len(re.sub('\W+',' ',string).split()))
#do not require split method here
letters = float(len(re.sub('\W+','',string)))
# need \w+ rather than \W+
Also , no need to typecast len as it will always give you integral values.
And do not have to worry about floating point division .
4/5 =.2 in python (already getting a float value )
0
string = input()
lis = string.split()
length = 0
for i in lis:
length += len(i)
print(round(length/len(lis)))
What's wrong with my code
I can't pass two test case
Anyone help me please?