+ 2

[SOLVED] What's the problem with this code?

Coach code link : https://sololearn.com/coach/73/?ref=app My answer : q = input() leni = len(q) inp = q.split(" ") avr = 0 for i in range (leni): avr += len(inp[i]) print(avr/leni)

28th Jan 2021, 8:46 AM
Sadra Shakouri
Sadra Shakouri - avatar
5 Respuestas
+ 6
Swift Your code should be like this but remember first you need to remove all special character except space from the string because you need to get average word length q = input() inp = q.split(" ") leni = len(inp) avr = 0 for i in inp: avr += len(i) print(avr/leni)
28th Jan 2021, 9:02 AM
A͢J
A͢J - avatar
+ 5
Swift Try this import re import math q = input() inp = q.split(" ") avr = 0 leni = len(inp) for i in inp: str = re.sub('[^a-zA-Z0-9\n\.]', '', i) avr += len(str) avg = math.ceil(avr / leni) print(avg)
28th Jan 2021, 9:11 AM
A͢J
A͢J - avatar
+ 3
leni = len(inp) because you want the length of the array not the string.
28th Jan 2021, 8:53 AM
jtrh
jtrh - avatar
+ 3
If you don't wish to use regex to filter the punctuation, then you might wish to try: import string punc = string.punctuation # which produces print(punc) Now you have a filter Good luck
28th Jan 2021, 9:37 AM
Rik Wittkopp
Rik Wittkopp - avatar
- 1
jtrh i think you didn't get what the code wants, did you see what the coach code wants?
28th Jan 2021, 8:58 AM
Sadra Shakouri
Sadra Shakouri - avatar