Could you tell me why it doesn't work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Could you tell me why it doesn't work?

input(words,int "x") def longer_than_x(words,int x): result[] for word in words: if(len(word) ge x) result.append(word) return result

13th Apr 2020, 1:04 PM
Torob4
Torob4 - avatar
6 Answers
+ 3
John Robotane your code contains errors, both syntaxic and logical ;) it would better works as: def longer_than_x(words, x): result = [] for word in words: if len(word) >= x: result.append(word) return result words = input().split(' ') x = int(input()) print(longer_than_x(words, x)) # expect words entry separated by space (change the split argument to use another separator)
14th Apr 2020, 1:08 AM
visph
visph - avatar
+ 4
Just by reading your code, I will say that you get the list of words shorter or equals to the integer? That is, you get words that you want filter out, and filter out those you want to keep... so, you've just to invert the condition: if x < len(word): That's all folks! :)
14th Apr 2020, 1:56 PM
visph
visph - avatar
+ 1
You haven't defined words
13th Apr 2020, 2:12 PM
Chinwendu Mere
+ 1
your code seem not to be in python. try this : def longer_than_x(words, x): result[] for word in words: if(len(word) >= x) result.append(word) return result words = input().split(' ') x = int(input()) print(longer_than_x(words, x))
13th Apr 2020, 3:24 PM
John Robotane
John Robotane - avatar
0
Dear visph and John Robotane thanks for your comments, but i still cant get proper out from my final code as below: print("please enter a list of words and an interger x then i will return a list of words that are longer than x") words=input().split(' ') print(words) x=int(input()) print(x) def longer_than_x(words,x): result=[] for word in words: if len(word) <= x: result.append(word) return result print(longer_than_x(words,x))
14th Apr 2020, 1:48 PM
Torob4
Torob4 - avatar
14th Apr 2020, 2:10 PM
Torob4
Torob4 - avatar