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!