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

Code Challenge solution not passing

I am not sure what to say because the code doesn't work and I'm not sure why. But any help would be appreciated.

I believe the problem with the code is in the expression: If even_odd(num) == True:

I tried using "True", True and it still did not pass.

I tested the even_odd(num) function and it returns either True or False

even.py
import random
start = 5
while bool(start) == True:
    num = random.radiant(1,99)
    if even_odd(num) == "True":
        print("{} is even".format(num))
    else:
        print("{} is odd".format(num))

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

2 Answers

Steven Parker
Steven Parker
231,269 Points

You have more than one issue. Here's a few hints:

  • "True" (with quotes around it) is a string, not a boolean value
  • "radiant" should actually be "randint"
  • You never need to compare a boolean against True, just use it directly
  • So your test can be simply "if even_odd(num):"
  • You don't need to convert a number to a boolean to check for zero, zero is "falsey"
  • So your loop can be simply "while start:"
  • Remember to reduce the value of start each time through the loop, or it will never end!

I'll bet you can get it now without an explicit spoiler. :wink:

Thank you Steven for the help. Those were great hint without giving too much away. I tried it again an it worked! I need to get used to the implicit Boolean values in the logic and looping statements. I think my original version was closer or a lot cleaner, because the version I posted looked sloppy.

I also found out I am supposed to have the function above or before the code being executed. Apparently you can only call a function if it comes before your code.

Please help me.