+ 3
Guys I have tried to complete the python average word length program a lot of times but I can't get the result!
Pls help me in getting the code ?
25 Antworten
+ 5
Ok... I didn't expect that at all!
You were a C programmer before, weren't you?
In python it's much simpler than that, given that the input consists of only alphabetic characters and single spaces, you can just
test = input()
words = len(test.split(" "))
spaces = words-1
print((len(test)-spaces)/words)
+ 5
The easiest language is harder to learn than the language u already learned.
Coding python while thinking C..🥺🥺🥺.
+ 4
Runtime Terror Have you ever heard of code golf? Might be something for you:
https://en.m.wikipedia.org/wiki/Code_golf
+ 3
https://code.sololearn.com/c1E7k4MWw1r7/?ref=app
+ 2
You can copy-paste it here or save it in code playground
+ 2
Hemalatha.M, this may shock you... That is the complete code...
+ 2
Runtime Terror Is that an attempt to present the most unreadable and least useful correct answer?
+ 2
First, you have to split the input string using space as separator, and get the count of number of words using len() method .ie;
text = input()
words = text.split(" ")
words_count = len(words)
Then, iterate over the string to count the number of characters (Excluding spaces)
You can use a condition like this:
count = 0
for i in text:
if not i == " ":
count += 1
Then now you can divide the word count with the number of characters and print the output
I hope this helps :-)
+ 2
Show your code
+ 2
Calvin Thomas I'll give you a better one-liner
Also, @ everyone, the problem consists of space separated words, so don't check for characters that are not there in the first place
Also, it wants the mean, not the ceil nor the floor of it
( lambda x: print(len("".join(x))/len(x)) )(input().split(" "))
+ 1
Can you show us one of your attempts?
+ 1
Yes but how?
+ 1
import string
punc= string.punctuation
test= input()
phrase=" "
count_letter =0
count_word =0
for i in range(len(test)):
phrase +=str(test[i])
print("character: " + phrase[i] + " ~ ")
if phrase[i] == " ":
count_word +=1
print("Adding a Word")
elif phrase[i] != " " and (phrase[i] not in punc): # not counting spaces and punctuations
count_letter +=1
print("Adding a Letter")
print(round(((count_letter)//count_word)+1))
+ 1
Yes
+ 1
Thank you guys
+ 1
Runtime Terror I do like one-liners... But that is unreadable
+ 1
Here's a one-liner possibility without any external modules:
(lambda x: print(int(k:=len(x.replace(" ",""))/(x.count(" ")+1))+(k>k//1)))("".join(x for x in input() if x not in ".,!?"))
# Hope this helps
+ 1
Angelo Yours doesn't seem to work.
+ 1
Calvin Thomas what input have you tested?