14/18 need help


#1

Here is the code i am using:

squares = [xˆ3 for x in range(1,11)]
filter(lambda squares: squares[30:70:], squares)

And whenever i try to run it, the complier gives me the following error:

Traceback (most recent call last):
  File "python/cli_runner.py", line 152, in <module>

What am i doing wrong?


#2

Restart your browser, and if this happens again chalk this one up as a bug.


#3

Still getting the same bug. i even tried using the following code:

squares = [xˆ3 for x in range(1,11)]
filter(lambda squares:70>= squares >=30 , squares)

But no results. What should i do? Just skip 14/18?


#4

Unfortunately I haven't even begun on Python, so really I shouldn't have tried to answer your question. But as for now you could just move on if it's possible for you to do so.


#5

It looks like you have the syntax for a lambda a bit off.
Here's my code:

squares = [x ** 2 for x in range(1, 11)]
print filter(lambda x: 30 < x < 70, squares)

and your code:

squares = [xˆ3 for x in range(1,11)]
filter(lambda squares: squares[30:70:], squares)

The only time you need to give the lambda the argument squares is at the very end. This specifies where the lambda is to retrieve the numbers it's supposed to filter through. The rest is just specifying the range of acceptable values (between 30 to 70).


#6

first of all this line

squares = [xˆ3 for x in range(1,11)]

well this ˆ is an invalid character and if you intended to use this ^ then what you would be doing is performing a binary xor shift of 5 on each of the values defined(that's what this ^ does it a bitwise operator) so you are not getting a list of squares
That line should be

squares = [x**2 for x in range(1,11)]

Now on this line

filter(lambda squares: squares[30:70:], squares)

you you define lambda with squares as your parameter awesome :wink: but wait the interpreter will halt here

squares[30:70:]

why because this is how list slicing works

#x[start:end:stride(or step)]
#where:
#x is a  given list
#start is a valid index in the list(int)
#end is a valid index in the list(int)
#stride is an integer that specifies how much i jump each time i am slicing

#for example
x = [x for x in range(10)] # which will be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

# I can get all odds in the list by doing this
print x[1::2]

so now your lambda function is giving the wrong indices in the list slicing. it should rather be this

print filter(lambda x:x >= 30 and x<=70, squares)

#7

For fun and giggles you could even rephrase that as follows to get rid of the AND:

print filter(lambda x: 30 <= x <= 70, squares)

#8

@tsluyter thank for the pointer. I did not know we could do that. :+1: