0

I need help

I don't know why this code didn't work I have error in test case4 s=input() a=s.lower() b=a.split() lst=[] a1=ord('a') alfa=[chr(i)for i in range(a1,a1+26)] for letter in a: if letter in alfa: lst.append(letter) avrg=len(lst)/len(b) if avrg==int(avrg): avrg=int(round(avrg)) print(avrg) else : avrg=int(round(avrg)+1) print(avrg)

6th Mar 2020, 9:39 PM
Abdollahi Mohammed
Abdollahi Mohammed - avatar
9 Antworten
+ 2
Btw you can check if its an alphabetical letter with isalpha() You dont need the alfa list
7th Mar 2020, 1:52 PM
Jnn
Jnn - avatar
+ 1
What is the purpose of your code and what are test cases?
6th Mar 2020, 10:37 PM
Jnn
Jnn - avatar
+ 1
What is the input? Is it a list of words or a sentence? Or just tell me the name of the challenge than i can see the task by myself
7th Mar 2020, 1:51 PM
Jnn
Jnn - avatar
+ 1
import math s = input().split() count=0 for word in s: count += len(''.join(i for i in word if i.isalpha())) print(math.ceil(count/len(s)))
7th Mar 2020, 4:42 PM
Jnn
Jnn - avatar
+ 1
I would have done it this way for example
7th Mar 2020, 4:43 PM
Jnn
Jnn - avatar
+ 1
1. You dont need to make it to lower case and then check for the ord number cause yoi can just use isalpha() as i said before 2.you can also just count the letters and not append it to a list and after that use the length 3.you dont need to care about it the value is already a whole number and not a floating number if you use math.ceil cause this method always rounds up
7th Mar 2020, 4:46 PM
Jnn
Jnn - avatar
+ 1
It work thank you jnn
7th Mar 2020, 8:03 PM
Abdollahi Mohammed
Abdollahi Mohammed - avatar
0
This is a challenge I want to get the average length of the words
7th Mar 2020, 5:33 AM
Abdollahi Mohammed
Abdollahi Mohammed - avatar
0
Challenge name is average length of words and the input is sentence
7th Mar 2020, 4:28 PM
Abdollahi Mohammed
Abdollahi Mohammed - avatar