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

This is a long challenge and I just want to make sure my syntax is correct so far.

If someone can look at this syntax so far and just tell me it's correct, that would be wonderful. Thank you

even.py
import random

start = 5

def even_odd(num):
    while start = true:
        random.randint(1,99)
    # If % 2 is 0, the number is even.
    # Since 0 is falsey, we have to invert it with not.
    return not num % 2

3 Answers

Donatas Ramanauskas
Donatas Ramanauskas
28,538 Points

Both the syntax and logic are not correct. The while loop should be outside even_odd function. You need to use even_odd function to check if nr is even or odd. You can't do this: while start = true, maybe you meant start == True.

Donatas Ramanauskas Thank you. Is this looking better? Am I getting closer?

 import random

start = 5

while True:
    num = random.randint(1,99)
    if num % 2 == 0:
        print("{} is even".format(num))
    elif num % 2 != 0:
        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
Donatas Ramanauskas
Donatas Ramanauskas
28,538 Points

a bit better, but still nr of things incorrect. While loop should be after the function, also you should never do this: while True, this starts infinite loop. The beginning of code should be:

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

to finish you need to write else action, that you already had in your code and decrement variable start each time the condition is checked.

Donatas Ramanauskas Thank you for that. I'm learning. So, how do I know when loop should be before, within, or after a function? Also, what does 'nr' mean?