Is_even


#1

is_even = "x"
if x % 2:
    return True
else:
      return False

when i click on save&submit it was shoeing me like this. could any one tell me where my code went wrong?

Oops, try again. Your code appears to raise an exception, see the console window for the error message!

File "python", line 3
SyntaxError: 'return' outside function


Is_even
#2

return can only be used inside a function? You don't have a function?


#3

can i use return statement like this
print True


#4

that is a print statement, not a return statement?

step 1 of the instructions is clearly to make a function:

Define a function is_even that will take a number x as input

Do you remember how to make a function? You can always revisit exercises you already completed, or check documentation


#5

hi i had written my program as for instructions. could any one tell me where my program went wrong?


#6

so on line 2 you have the following code:

if x % 2:

so lets say x = 24, 24 %2 is zero.

so if numbers are even, the remainder is zero, so we should if the remainder equals (==) zero


#7

hi
thank you i got the code. i did understand why we have to modify this step as follows.
if x % 2 == 0:

and when we pass the if statement like this. whey cont program is executed

if x % 2:

could u please explain me?
.


#8

it not that we can't do, but its slightly more complicated.

For even numbers the remainder is zero, for odd number the remainder is one (makes sense, right?)

So then if will evaluate the following:

if 0:

for even numbers, and:

if 1:

for odd numbers.

in python, 0 is considered false, and 1 is considered true. So this would mean, you need to swap return true and return false


#9

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.