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 don't understand what is wrong!

It gives a bummer! It looks like Task 1 is no longer passing!

What exactly is wrong with the code I have written?

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

2 Answers

Keith Whatling
Keith Whatling
17,752 Points

Hi,

Almost perfect, just need to add () around your print statements.

We have all done it, and will no doubt continue to do it!. I have found it a good idea to try the following.

Copy and past your code to python and test it. Take your coding hat off, and check it like you would another person's letter or email, looking for typos, grammar and spelling mistakes. sometimes walking away, having a coffee, coming back and physically read the code will help. It helps me anyway. (Did you notice I did not capitalise the S in sometimes, I didn't until I reread the text) :).

There is an old adage its easier to write new code to fix a problem that it is to read old code and amend/fix it.

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

Thanks ! :)

Keith Whatling
Keith Whatling
17,752 Points

No probs, i just did the challenge again, you can write it like this using 0 as falsey and anything else is true.

Note the use of start and the returned value from even_odd.

You're doing great keep going!

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

That's even better! Thanks again!