+ 1
HELP ME ON THIS I tried exactly 12 times below is my attempt
Given a sentence as input, calculate and output the average word length of that sentence. To calculate the average word length, you need to divide the sum of all word lengths by the number of words in the sentence. Sample Input: this is some text Sample Output: 3.5 Explanation: There are 4 words in the given input, with a total of 14 letters, so the average length will be: 14/4 = 3.5 https://code.sololearn.com/cRUwXODr7Ym0/?ref=app
9 odpowiedzi
+ 4
There's a few issues that I can see. Firstly, for languages like Python, where you *can* give your variables nice, descriptive names, it's always a good idea to do so. Instead of calling a variable "x", give it a name that describes what it does or is. For a bit of code that's this short, it doesn't *really* matter, but it's good practice when you start writing longer stuff. If I have a project that's a thousand or so lines of code, it's a lot nicer for me (and my fellow devs!) if I have variables named stuff like "wordInputLength" rather than "x".
Secondly, your original "total" variable includes spaces. That doesn't really make sense here, since we're asked for the average *word* length (which does not include spaces, by definition). There are two ways to figure this out:
1) Join all the words together ("this is some text" --> "thisissometext"), and get the length of that string.
2) Get the total length of the input string, and subtract the number of spaces. Recognize that the number of spaces is necessarily the number of words minus one (so if we have four words, we need to subtract 4-1 = 3 spaces).
Here's my edited version:
https://code.sololearn.com/ca8a9A106a24
+ 5
I did in a different way:
text = input()
text_length = len(text.split())
text2 = ''.join(text.split())
letters = len(text2)
average = letters/text_length
print(average)
+ 3
Scarlet witch
Awesome everything was correct but I didn't use join
+ 3
Codemurai thanks and I even don't know why I used join 😅
+ 1
Sololearn wants the output rounded up to the nearest whole number
+ 1
But how I tried please help
+ 1
I don't know this code It didn't teach me this
+ 1
0
import math
n = 5.5
n_up = math.ceil(n)
print(n_up)
# output
6
PLUS: thats not even your code