Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics Functions and Looping Functions

"Create a function" practice exercise This is what I was asked to do, but I can't figure out if I'm on the right track.

Challenge Task 1 of 1 Let's create a function that determines if a specific value is odd.

Let's name the function is_odd and have it declare a single parameter.

It should return True if the value is not divisible by 2.

Here is a link to my code in workspaces:

https://w.trhou.se/m6cldzxrai

open up the 'create_a_function.py' file

I still cannot get this, can you help?

Thank you

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 67,987 Points

You have the general idea. There are some syntax issues to clean up:

def is_odd(number,2):  # do not need to pass in constant, remove ", 2"
    if 0 = (number % 2)  # if needs trailing colon (:), compare uses double equal (==)
        print("False")  # should return False, not print it
    elif 1 <= (number % 2):  # a condition uses 'elif', without condition use 'else'
        print("True")  # should return True, not print it

Running the fixed code in the REPL:

$ python
Python 3.6.3 (default, Oct  3 2017, 21:45:48) 
[GCC 7.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> def is_odd(number):
...     # your fixed code here
...     pass
... 
>>> is_odd(4)
False
>>> is_odd(5)
True

Post back if you need more help. Good luck!!

Thanks Chris! That was very helpful!