0

Problem: Average Word Length (Python, Medium)

The following code doesn't pass hidden test cases 3 & 5 for some reason. Can someone tell me how I can correct this code? Thank you. https://www.sololearn.com/coach/73?ref=app from math import ceil input_string = input() lengths = [] count = 0 for letter in input_string: if letter.isalpha(): count += 1 else: if count > 0: lengths.append(count) count = 0 avg = ceil(sum(lengths) / len(lengths)) print(avg)

26th Jun 2024, 11:53 AM
Anudeep
Anudeep - avatar
2 ответов
0
@Lisa I did that already by using isalpha(). Anyway, I found the problem. The conditional block I used, doesn't account for the case when the input string ends in in an alphabetic character. Fixed this by adding ``` if count > 0: lengths.append(count) ``` outside the for loop. It passes all the test conditions. I wonder if there's a more efficient way to code this.
26th Jun 2024, 12:06 PM
Anudeep
Anudeep - avatar
+ 1
The instruction asks you to "Remove all punctuation."
26th Jun 2024, 11:55 AM
Lisa
Lisa - avatar