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

Christopher Parke
Christopher Parke
21,978 Points

Even_odd

I'm not sure what I'm missing here...

even.py
import random

start = 5

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

while start > 4:
    number = random.randint(1, 99)
    if even_odd(number) % 2 != 0:
      print("{} is even".format(number))
      start = start -1
    elif even_odd(number) % 2 = 0:
      print("{} is odd".format(number))
      start = start -1

2 Answers

Steven Parker
Steven Parker
231,248 Points

Your while loop should run until start is "falsey". Yours runs while start > 4 (just one time).

:point_right: You can check for "truthyness" by just naming a variable.

The even_odd function gives you True for even numbers, False for odd ones.

:point_right: You won't need to do any comparisons or math on the result, just test it.

:point_right: Lastly, if you've already tested for even, you can use a plain "else" to handle odd.

Kourosh Raeen
Kourosh Raeen
23,733 Points

A few points:

1) start would be falsey once it's decremented down to zero so just use while start

2) The even_odd() function tells you whether a number is even or odd so you don't need to use %2 in the loop. Just write

if even_odd(number):

3) If it is not even then it is odd so no need for elif. Use else instead.

4) Decrement start just once at the end of the loop