Guys I have tried to complete the python average word length program a lot of times but I can't get the result! | Sololearn: Learn to code for FREE!

+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 ?

7/26/2021 3:58:32 PM

Hemalatha.M

28 Answers

New Answer

+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

Angelo Simon Sauter actually my kitten stood on my keyboard ⌨️ haha

+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

print((lambda i:__import__("math").ceil(len([_ for _ in i if not _ in __import__("string").punctuation and not _ == " "])/len(i.split(" "))))(input()))

+2

~LoneWolf yes Python it's not only easier, it's a completely different way of thinking

+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(" "))

+2

Angelo Why do you think that's better lol 😆

+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

+1

Thank you guys

+1

Runtime Terror I do like one-liners... But that is unreadable