0

Print number of words which are greater than 5 letters in a sentence.

If for eg. a sentence: "This is Sololearn Mobile App". Output should be "2" ("Sololearn" and "Mobile" have letters greater than 5). I have tried using List Comprehension in many ways, I ended up getting number of letters in a sentence/word or getting an error. First, I got atleast length of the word. Now, I am not even getting anything and just ruined up everything. This is really funny to me. I also have searched in many sites, but I can't find? The code I did is below; and I lost the understanding of using functions, List comprehension and other coding thing. text= input("enter").split() def greater_5(text): letters= [words[0:] for words in text if str(len(words[0:])>=5)] return str(len(lett)>=5) print(greater_5(text)) I know this so wrong and don't know what I am doing?

4th Feb 2019, 3:25 PM
Prithvi
11 odpowiedzi
+ 7
print(len([w for w in input().split() if len(w) > 5]))
4th Feb 2019, 3:51 PM
Anna
Anna - avatar
+ 6
Try this: text = input("enter").split() def greater_5(text): letters = [1 for word in text if len(word)>5] return sum(letters) print(greater_5(text)) PS: You can definitely make this shorter.
4th Feb 2019, 3:40 PM
Diego
Diego - avatar
+ 5
b = 0 a = input().split(" ") for i in range(len(a)): if(len(a[i]) >= 5): b += 1 print(b)
4th Feb 2019, 3:50 PM
Jax
Jax - avatar
+ 3
Prithvi Anna's answer adds a word to the list if its length is greater than 5. Then counts the len of that list and returns that value. Mine adds a "1" (instead of the word itself) to the list if the length of a word is greater than 5 and then sums all those "1".
4th Feb 2019, 4:25 PM
Diego
Diego - avatar
+ 2
Anna I was thinking about: print(sum(len(w) > 5 for w in input().split())) But your approach is quite interesting.
4th Feb 2019, 4:28 PM
Diego
Diego - avatar
+ 2
Prithvi 1. This isn't the problem but use "words" instead of "words[0:]". 2. "if str(len(words)>=5)" always returns True. Use "if len(words) >= 5". 3. "return str(len(letters)>=5)" doesn't return the length of "letters". Use "return len(letters)".
4th Feb 2019, 4:38 PM
Diego
Diego - avatar
+ 1
Diego Acero Thankyou! And Wow! You did so quickly and I thought "sum" won't work here. Can you just tell why in "letters", there's "1" and how come "sum"?
4th Feb 2019, 3:50 PM
Prithvi
+ 1
Anna Your's code is amazing. See, I missed the logic here. I used "str(len(some_variable)>=5)" something like this, totally ruined my idea. But you, yay! Thankyou!
4th Feb 2019, 4:01 PM
Prithvi
+ 1
Diego Acero Why did you took "1" in letters = [1 for word in text if len(word)>5] and what is it doing? And also 'sum(letters)'.
4th Feb 2019, 4:20 PM
Prithvi
+ 1
Hey, you all really rock with code and made me continue to learn python again. Thankyou all! Also, can anybody please tell me, what was going wrong in the code I wrote!
4th Feb 2019, 4:31 PM
Prithvi
0
Jax Thankyou! Also worth it. I tried somewhat same like you but I used "import count", "counter" which unfortunately didn't worked for me. Maybe my idea was not good. I first thought of getting words>=5 and then looked for getting their lengths using "len". Pfft! Damn it!
4th Feb 2019, 4:06 PM
Prithvi