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

I'm almost sure this is correct can anyone help?

I believe I'm having a logic issue, or perhaps even a misunderstanding of boleans....can anyone tell me whats wrong with this code? Ive looked at several others who had their code typed similarly but to no avail I'm still not seeing my error.

Thanks in advance

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 == True:
    num=random.randint(1,99)
    if even_odd(num) ==0:
        print("{} is even".format(num))
    else:
        print("{} is odd".format(num))
    start-=1

5 Answers

Hey Reign, The problem you are having is on line 9: if even_odd(num) ==0:. This means that if the number is False, since 0 equates to False in Python. To fix this, remove the == 0 part of the code. This means print("{} is even".format(num)) will occur if the number is even. Ask me to clarify if you need. Hope this helps.

Hey Aaron I did as you recommended and I'm still getting an error message. I removed the ==0...but the code's still not working.

What exactly is the error message? Could you also post what your current code looks like?

the error message is Wrong number of prints

import random start=5 def even_odd(num): return not num %2 while start==True: num=random.randint(1,99) if even_odd(num): print("{} is even".format(num)) else: print("{} is odd".format(num)) start-=1

I got it! Just remove the == True on the while loop. I don't know why it doesn't work with it, but that fixes it.

Hey Aaron that still didn't work for me....could this be a server issue??

import random start=5 def even_odd(num): return not num %2 while start: num= random.randint(1,99) if even_odd(num)== True: print("{} is even".format(num) else: print("{} is even".format(num) start-=1

Also I put the even_odd(num) function in an idle just to be sure of what it was returning, and of course I got a bol. Can you give me an indication of what part of the function causes the bol return? I'm believe its the not keyword but I'm not sure. If it is an explanation would be most appreciated.

I just passed the challenge with this code:

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

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

A problem you had was in the else statement. You put print("{} is even".format(num)), when it should have been print("{} is odd".format(num)). This meant that the program said that every number was even.

Actually that was just my mistake in copying the code....it says odd in the actual challenge but still wont pass.

I assume you mean bool, not bol. To answer your question, I'll give you an example. If you do even_odd(4), the function should return True. If we look inside the function, it would return not 4 % 2. The remainder of 4 / 2 is 0. Remember 0 is falsey and 1 is truthey. The function would then do not 0 or not False, which is equal to True.

HA! I found my error thanks so much for you help!

Your welcome! What exactly was the error?

In my most recent attempt I ended with start-=5 as oppose to one.....honestly though there are still a couple of aspects of that challenge that have me a bit baffled and wondering if I thoroughly understand the entire logic of the challenge... simply because of how many times I had to re-try the code.