15 is_even


#1

I am trying to make the function print "Even" or "Odd" but cannot.

x = raw_input()

def is_even(x):

if x % 2 == 0:
    print "Even!"
    return True
else:
    print "Odd!"
    return False

#2

You need something between the brackets, like so:

x = raw_input("Pick a number 1-100")

#3

It still won't print.

The code "passes" and executes without error as I had it but it still won't print "even" or "odd".


#4

Can I see your full updated code, please?


#5

I think I need to call the function somehow?

x = raw_input("Number to test: ")

def is_even(x):

if x % 2 == 0:
    print "Even!"
    return True
else:
    print "Odd"
    return False

#6

Try

is_even(10)

At the bottom. It should be even.


#7

Thanks.

But doesn't that defeat the purpose of asking for raw_input?

I want to have the user enter a number and print even or odd. Not just return true or false.


#8

I got it this far... but it always prints "even"

x = raw_input("Number to test: ")

def is_even(x):

if x % 2 == 0:
    return True
else:
    return False

if is_even:
print "Even"
else:
print "Odd"


#9

Make it

If is_even == True:

#10

won't work because is_even() is a function that expects and argument.

if is_even(x):
    print "Even"
else:
    print "Odd"

should work.


#11

try this for the raw input processing

def is_even(x):
    if x % 2 == 0:
        return "Even!"
        #return True
    else:
        return "Odd"
        #return False


x = raw_input("Number to test: ")
#the user could enter a non-digit
print is_even(int(x)) if x.isdigit() else None

Or you can return a tuple with a boolean and a string

def is_even(x):
    if x % 2 == 0:
        return True,"Even!"
    else:
        return False, "Odd"

x = raw_input("Number to test: ")
#the user could enter a non-digit
print is_even(int(x)) if x.isdigit() else None

you can shorten your code to this

def is_even(x):
    return not x%2

x = raw_input("Number to test: ")
#the user could enter a non-digit
print is_even(int(x)) if x.isdigit() else None

#12

guys i think u are taking this too far, the question says:
Define a function is_even that will take a number x as input.
If x is even, then return True.
Otherwise, return False.
this is just the simple code i used that worked:
def is_even(x):
if x % 2 == 0:
return True
else:
return False


#13

what you just wrote is the same code pesented here

which is is just as simple as what you suggesting, The raw_input part is his way of testing the correctness of the method


#14

I answered here has well..

numbers = [1,2,3,4,5,6,7,8,9];

def is_even(number):
    if number % 2 == 0:
        print number, "is even"
        return True
    else:
        print number, "is odd"
        return False

for number in numbers:
    is_even(number)

#15

Continuing the discussion from 15 is_even:

Hey, I think I understand what you are getting at. I believe you are missing the call to function (hopefully term is right).. Going off your OP:

x = int(raw_input())
def is_even(x):
    if x % 2 == 0:
        print "Even!"
        return True
    else:
        print "Odd!"
        return False

print is_even(x)

raw_input() converts input to a string and adding int() for an integer. Hopefully that helps to the question you were asking.


#17