FAQ: Lambda Function Code Challenge - Even/Odd

This community-built FAQ covers the “Even/Odd” exercise from the lesson “Lambda Function Code Challenge”.

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

FAQs on the exercise Even/Odd

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!

my solution:
even_or_odd = lambda num: “odd” if num%2 > 0 else “even”

I originally was trying to define “even” first but kept getting errors because I was defining “even” by num%2 = 0. The system did not like the equal sign within the function.

From there, I was able to find this solution as well:
even_or_odd = lambda num: “even” if num%2 < 1 else “odd”

I did the same thing. Try using ‘==’ instead of ‘=’ in the lambda function. The below solution worked for me:

even_or_odd = lambda num: “even” if num%2 == 0 else “odd”

My first go was: even_or_odd = lambda num: “even” if num%2 = 0 else “odd”. This was unsuccessful because of the single ‘=’. A double ‘==’ works. Why is this?

“=” is used to assign a value to a variable. “==” is used instead for comparison

Hello Codecademy Forum,

#Write your lambda function here
even_or_odd = lambda foo: “even” if foo % 2 == 0 else “odd”
print even_or_odd(10)
print even_or_odd(5)

Why do we not need to use parenthesis here to print out the lambda functions? Is this because this course was designed using an older version of Python?

Thanks.

Yes print was a statement rather than a function in earlier versions of Python. Once it was set up as a function (3.x) it was made available to older Python2 versions with from __future__ import print_function.

Generally that import removes the option to use print as a statement but most codecademy lessons, even those that execute a script with Python2.x seem to accept print as both a function and a statement.

hello lovely people!
can someone help me identify why this is wrong? or point, if relevant what should be removed/changed/added?
thank you!!!

even_or_odd = lambda num: “even” if num%2 else “odd”

error:

If the input is odd, return "odd"

What is the outcome of num % 2? Using logic like this is a test of truthiness of the object, so you’re effectively doing the same as bool(num % 2).

Once you’re sure about the output of num % 2 for various values you should check what happens when you do your truth_value_testing