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

still stuck - moved the while loop

I moved the while loop. I thought I fixed the indent correctly - this is v frustrating.

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
        rnum = random.randit(1,99)
         if even_odd(rnum):
            print("[]" is even".format(rnum))
        else:
            print("[]" is odd".format(rnum))
        start -=1          

1 Answer

Indent the while loop outside the function

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:  # mist ":"
    rnum = random.randint(1,99)  # is randint not randit
    if even_odd(rnum):
        print("{} is even".format(rnum))  # for placeholder use "{}"
    else:
        print("{} is odd".format(rnum))  # for placeholder use "{}"
    start -= 1

I fixed the errors that you pointed out, thank you. But I still get - the first = IMPORT RANDOM - Task 1 is no longer passing.

I saw references to moving the while loop under the function, and I thought with the indents that this is what I did. now I am confused.

I edit my post. "randint" not "randit". I did not see that :)

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

am still getting the Task 1 not found error

your code

if even_odd(rnum):
    print(""{}" is even".format(rnum))
else:
    print(""{}" is odd".format(rnum))

my code

if even_odd(rnum):
    print("{} is even".format(rnum))
else:
    print("{} is odd".format(rnum))

the placeholder don't need special quotes

if I change to

if even_odd(rnum): print("{} is even".format(rnum)) else: print("{} is odd".format(rnum))

I get the Bummer! Wrong number of prints. error.

I tried this code and it works

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