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 (2015) Number Game App Even or Odd

hi...need help with this code. thanks

hi...need help with this code. thanks

even.py
import random
secret_num=random.radnint(1,10)
while True:
    even_odd=int(input("Put a number"))
    if even_odd==secret_num:
        print("YouGoIt MyNumberwas{}".format(secret_num))
        break
    else:
    even_odd_1=even_odd/2
    if int(even_odd_1)=even_odd_1:
        print("Number is even")
    else:
        print("Number is not even")

6 Answers

Steven Parker
Steven Parker
216,164 Points

:point_right: It looks like you may have copied from a different challenge (or two!).

You might want to read the challenge instructions again carefully. Some of what it asks for is very different from what you wrote:

  • the challenge asks you to write a function
  • your function will require and test a passed argument
  • it will not need to input anything
  • it will not need any random value
  • the function will return the result
  • the results will only be True and False
  • it will not need to print anything

Try again, and be careful to do only what the instructions ask for.

vikas pal
vikas pal
11,207 Points

your randint spelling is wrong in line 2

random.randint(1,10)
Steven Parker
Steven Parker
216,164 Points

:warning: Knowing the correct spelling of randint might be useful in the future, but it will not help this challenge.

thanks Steven

does this looks better?

def even_odd(number): number=6 number_1=6/2 if int(number_1)==number_1: True else: False

Robin *
Robin *
5,670 Points

There is no need to define the number inside the function. The number is passed to the function when it is called for example: even_odd(3) where 3 is the 'num'

You should use a modulo operation in order to figure out if the number has a rest value or not. In python % is the operator for a modulo operation. for example: 5 % 2 = 1 It is 1 because 5 divided by 2 leaves a quotient of 2 and a remainder of 1.

If you are still reading this then you might have realized that if you take an odd number and MOD by 2 you will get a value that is "Truthy" and if you take an even number and MOD by 2 you will get a value that is "Falsy". So our even_odd function we have to take that in to consideration.

We can achieve this by using the not operator.

def even_odd(num):
    return not num % 2

We could also do it like this

def even_odd(num):
    if num % 2 == 0:
        return True
    else:
        return False

thanks