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
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)
+ 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! :)
+ 1
You haven't defined words
+ 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))
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))
0
thanks : ))
â
https://code.sololearn.com/clnt8Gk3KiA0/?ref=app