+ 1

Why does the test cases 3 and 5 are not working properly????

n = input().split(" ") a = 0 for i in n: b = len(i) a = a+b x = len(n) print(round(a/x)) Here is the question: You are in a college level English class, your professor wants you to write an essay, but you need to find out the average length of all the words you use. It is up to you to figure out how long each word is and to average it out. Task: Takes in a string, figure out the average length of all the words and return a number representing the average length. Remove all punctuation. Round up to the nearest whole number. Input Format: A string containing multiple words. Output Format: A number representing the average length of each word, rounded up to the nearest whole number. Sample Input: this phrase has multiple words Sample Output: 6

4th Jul 2021, 1:02 PM
Shahir
Shahir - avatar
13 Réponses
+ 4
Shaik.Shahir , to get a useful help from the community, please also provide a clear task description. thanks!
4th Jul 2021, 1:06 PM
Lothar
Lothar - avatar
+ 4
Shaik.Shahir , the task description says, that the output has to be rounded up to the nearest whole number, but you applied round(). this will result in rounding up OR down depending on the value you wanted to process. to do it according the description you need to apply the ceil() function. to do so, you need to import the "math" module. you also have to remove all punctuation characters. happy coding and good success!
4th Jul 2021, 1:15 PM
Lothar
Lothar - avatar
+ 3
Shaik.Shahir , from which python tutorial is this task? the removing of the punctuation is still missing...
4th Jul 2021, 1:56 PM
Lothar
Lothar - avatar
+ 2
When I am running this code though I am getting correct output it failes in test case 3 and 5 and I do not know why??
4th Jul 2021, 1:10 PM
Shahir
Shahir - avatar
+ 2
import math n = input().split(" ") a = 0 for i in n: b = len(i) a = a+b x = len(n) print(math.ceil(a / x)) But when I try this I am getting the test caes 1 get failed
4th Jul 2021, 1:25 PM
Shahir
Shahir - avatar
+ 1
Shaik.Shahir Your code missing : 1)Remove all punctuation. 2) Round up to the nearest whole number. i think it asking round up so you may need ceil(a/x)
4th Jul 2021, 1:15 PM
Jayakrishna 🇮🇳
+ 1
Shaik.Shahir you have remove punctuation marks from input if any first.. and count remainings. Ex: abcd@efg to abcdefg
4th Jul 2021, 2:00 PM
Jayakrishna 🇮🇳
+ 1
Okkk
4th Jul 2021, 2:06 PM
Shahir
Shahir - avatar
+ 1
Then what about this import math n = input().split(" ") a = 0 for i in n: if i not in "@#&+()/?": for x in i: b = len(x) a = a+b x = len(n) print(math.ceil(a/x))
4th Jul 2021, 2:28 PM
Shahir
Shahir - avatar
0
import math import re w = re.sub(r"\W", " ", input()).split() a = sum(map(len, w))/len(w) print(math.ceil(a)) # split default separator is space # regex \W match not word chars (ascii alphanumerics + underline) # re.sub replace pattern with substitute string in input string # sum return sum of iterable # map return iterable mapped with function
4th Jul 2021, 5:02 PM
visph
visph - avatar
0
if I not in "@#&+/?" : It won't work because here I have values "words" , not individual charecters. But you need charecters in 'i' here. So take it into inner for loop, so x will have charecters only and then if x not in"@#&+()/?" Works fine . Again here you missing some punctuations "%$!.." add those.. edit: Shaik.Shahir check this only after your tried again import math n = input().split(" ") a = 0 for i in n: for x in i: if x not in "@#&/?%!
quot;: #b = len(x) a = a+1 x = len(n) print(math.ceil(a/x))
5th Jul 2021, 12:15 PM
Jayakrishna 🇮🇳
- 1
u=input("") is_lower = u.lower() is_lower1 = is_lower.split() length=len(is_lower) total=len(is_lower1) print("Average word length : " , length/total) try this
5th Jul 2021, 5:05 PM
Somil Khandelwal