FAQ: Lambda Function Code Challenge - Double Or Zero

This community-built FAQ covers the “Double Or Zero” exercise from the lesson “Lambda Function Code Challenge”.

Paths and Courses
This exercise can be found in the following Codecademy content:

FAQs on the exercise Double Or Zero

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

I receive a syntax error on line 2 when running this:

#Write your lambda function here
double_or_zero = lambda num: num*2 if
num > 10 else 0
print double_or_zero(15)
print double_or_zero(5)

What is wrong with the script?

*edit: Although the example provided in the lesson had a line break where I put mine (after “if”), the issue was that I needed the entire lambda function on one line. Bringing the rest up on line 2 after “if” fixed the error. It’s a bit confusing to include an example with a line break without explaining that.

So I tried:

double_or_zero = lambda num: num * 2 if num > 10 else return 0

and this gave an ‘invalid syntax’ error

after looking at the solution it shows that the ‘return’ statement is unnecessary, so it should look like:

double_or_zero = lambda num: num * 2 if num > 10 else 0

Can someone explain why this is?

The lambda returns one value or the other, implicitly. To illustrate we could use the MAP function…

>>> double_or_zero = lambda num: num * 2 if num > 10 else 0
>>> nums = [5, 10, 15, 20, 16, 12, 8, 4]
>>> map(double_or_zero, nums)
[0, 0, 30, 40, 32, 24, 0, 0]
1 Like