I don't know what's wrong with this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I don't know what's wrong with this code?

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 My code: sentence = input("") words = sentence.split(" ") sentence = sentence.strip() average = len(sentence) // len(words) print(average)

29th Nov 2020, 6:08 AM
K.S.S. Karunarathne
K.S.S. Karunarathne - avatar
1 Answer
+ 4
You need to get the total of the lengths of each word and divide by the number of words. Make sure to round up! Right now your taking the total length of the sentence (including spaces) and dividing by the number of words, without rounding up.
29th Nov 2020, 6:47 AM
ChaoticDawg
ChaoticDawg - avatar