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)
2 Answers
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.
+ 1
The instruction asks you to "Remove all punctuation."