Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Basics (2015) Letter Game App Even or Odd Loop

Daniel Deiana
Daniel Deiana
11,341 Points

The answer seems to be correct when i'm typing it into workstation script

The script I've used seems to be correct when I run it through Workstation but "check work" doesn't think so, what am I missing? regards Dan

even.py
import random

def even_odd(num):
    # If % 2 is 0, the number is even.
    # Since 0 is falsey, we have to invert it with not.
    return not num % 2
    if num % 2 == 0:
        return True
    else:
        return False

start = 5

while start > 0:
    number = random.randint(1,99)
    even = even_odd(number)
    if even == True:
        print("{} is even.".format(number))
    else:
        print("{} is odd.".format(number))
    start -= 1

2 Answers

andren
andren
28,558 Points

The problem is that the challenge checker is extremely picky about text output, if you don't print the exact thing it expects it will usually fail you, the challenge asks you to print "{} is even" and "{} is odd", notice that there is no period at the end of those strings, the fact that you end your messages with a period is what trips the challenge checker up, remove those periods and you will be able to pass the challenge.

On other thing I will point out though is that while it won't cause any errors most of the code in the even_odd function is redundant, the first line of code and the if...else statement does the exact same work, and once a function hits a return statement it stops the function and returns the value. Which means that any code in the function after the first return statement will never run, so you can remove the if...else statement entirely from the function and nothing would actually change.

Daniel Deiana
Daniel Deiana
11,341 Points

Fantastic thanks for your help, I worked out the periods in the end. And thanks for your code tips.