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

Viktor Stärn
Viktor Stärn
1,876 Points

even_odd while loop Challenge Task 3 of 3 problem

I had no problem with task 1 and 2 but after I coded the while loop the 'check work' would only ever tell me that task 1 was no longer going through. I wrote the code in workspace to get better trouble shooting; changed a few things to make it run properly, and checked it again. But now it only gives me 'Bummer! Try again!' (After trying different things, it seems that having the 'start' variable outside my function is causing the 'Task 1 no longer going through' message, but inside it goes back to 'Bummer! Try again!')

Not really sure what to change as I am sure the code runs, having ran it in workspaces. As there aren't any other questions about this challenge I feel like I'm probably missing something incredibly obvious. :P

Any help would be greatly appreciated. :)

even.py
import random
num = 0

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

def prev(num):
    start = 5
    while start != 0:
        start = start-1
        num = random.randint(1, 99)
        renum = even_odd(num)
        if renum == True:
            print('{} is even!'.format(num))
        else:
            print('{} is odd!'.format(num))

prev(num)

1 Answer

You shouldn't make a function. Also, since code challenges are super picky, you must remove the exclamation at the end of both strings.

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 != 0:
    start = start-1
    num = random.randint(1, 99)
    renum = even_odd(num)
    if renum == True:
        print('{} is even'.format(num))
    else:
        print('{} is odd'.format(num))
Viktor Stärn
Viktor Stärn
1,876 Points

You are completely right, thank you!