Cody Munster1,630 Points
Trying to find an odd/even number using a function
I am stumped on this. I have run this as a program outside of a function and it returns a True for an even number and a False for an odd number. What i don't understand is why it won't work here?
The only thing i can think of is it has something to do with the argument in the function?
def even_odd(num): x = int(input("Please enter a number: ")) num = (x % 2) if num == 0: print(True) else: print(False)
Manish Giri16,265 Points
There are some problems with your code.
Look at the first instruction - "Write a function named even_odd that takes a single argument, a number.". You need to check whether this argument is even or odd. This argument is represented in your code by the variable
numin this line -
Now look at these lines -
x = int(input("Please enter a number: ")) num = (x % 2)
You're taking a input from the user, and assigning it to
num. Thereby, you're overwriting the argument that was actually passed in to your function. Instead of this, you should directly be checking if
num % 2is 0 or not.
For the final step, the challenge asks you to "return" a value - "return True if the number is even, or False if the number is odd.". You're instead printing
if num == 0: print(True) else: print(False)