Python Python Basics (2015) Number Game App Even or Odd

Cody Munster
Cody Munster
1,630 Points

Trying to find an odd/even number using a function

Hi Guys,

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?

Thanks

even.py
def even_odd(num):

    x = int(input("Please enter a number: "))
    num = (x % 2)

    if num == 0:
        print(True)
    else:
        print(False)

1 Answer

Manish Giri
Manish Giri
16,264 Points

There are some problems with your code.

  1. 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 num in this line - def even_odd(num):.

  2. 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 % 2 is 0 or not.

  3. 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 True or False -

    if num == 0:
       print(True)
    else:
        print(False)
    
Cody Munster
Cody Munster
1,630 Points

Thanks a lot Manish. I ended up submitting the following

def even_odd(num):

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

Your help is much appreciated!

Christian A. Castro
Christian A. Castro
18,829 Points

Manish Giri Any idea what I might be doing wrong on my end?

import random

start = 5;
def even_odd(num):
    while start:
        num = random.randint(1,99)
        if (num % 2) == 0:
            print("{} is even ").format(num)

        else:
        # Since 0 is falsey, we have to invert it with not.
            print("{} is odd ").format(num)
        start = start - 1;
Manish Giri
Manish Giri
16,264 Points

Christian A. Castro The problem in your code is the placement of the start variable. If I run your code, I get this error -

NameError: local variable 'start' referenced before assignment

That's because in the even_odd function, start is considered as a local variable. You're using start to execute a while loop, but inside the function, there's no local variable start with a value assigned to it, before it's used. Python doesn't know you're trying to refer to the global variable start.

I'd recommend using this approach -

def even_odd(num):
    # inside the function, just return True or False based on whether num is even or odd

start = 5
while start:
    # loop until start is 0
    # generate the random number and use the even_odd function to check if it's even or odd, 
    # and print accordingly