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

Why does it say it tell me wrong number of prints???

The runs 5 times and stops, so what is the problem?? Why does it say "wrong number of prints?

even.py
import random
start = 5
while start !=0:
    start-=1
    num=random.randint(1,99)
    x=num%2
def even_odd(num):
    if x !=1:
        print(num, 'is even')
    else:
        print(num, 'is odd')
even_odd(num)
    # Since 0 is falsey, we have to invert it with not.
    #return not num % 2

1 Answer

Nicholas Ward
Nicholas Ward
5,797 Points

Just two quick changes should fix it.

  1. You are calling the even_odd() function outside of your while loop, so the while loop is running and not really doing anything, then even_odd() is being called only once. You need to switch the order of your while loop and even_odd() so that you can call the function in the loop, then move the even_odd() call into the loop.

  2. You want to calculate x inside of even_odd

import random
start = 5

def even_odd(num):
    x = num % 2
    if x !=1:
        print(num, 'is even')
    else:
        print(num, 'is odd')

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

Hope that helps!