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

Zac Longanecker
Zac Longanecker
1,900 Points

Challenge task: even.py

import random start = 5

def even_odd(num): # If % 2 is 0, the number is even. if num % 2 == 0: print("{} is even").format(num) # Since 0 is falsey, we have to invert it with not. return not num % 2 else: print("{} is odd").format(num)

while start > 0: num = random.randint(1, 99) even_odd(num) start -= 1

even.py
import random
start = 5

def even_odd(num):
    # If % 2 is 0, the number is even.
    if num % 2 == 0:
        print("{} is even").format(num)
        # Since 0 is falsey, we have to invert it with not.
        return not num % 2
    else:
        print("{} is odd").format(num)


while start > 0:
    num = random.randint(1, 99)
    even_odd(num)
    start -= 1

2 Answers

AJ Salmon
AJ Salmon
5,675 Points

Printing formatted strings should look like this:


>>> print("Example string number {}".format("1"))
'Example string number 1'
#.format is called inside of the print function, and enclosed in its parentheses

Also, this challenge wants you to call even_odd and check it inside the while loop. So It should follow this structure:


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:
    ran_num = random.randint(1, 99)
    #check to see if even_odd(ran_num) is == to 0. If it is:
        print("{} is odd".format(ran_num))
    #otherwise:
        print("{} is even".format(numb))
    #then decrease start by 1

Hope this helps!

AJ Salmon
AJ Salmon
5,675 Points

Happy to help :)