Python Python Basics (2015) Letter Game App Even or Odd Loop

Jaroslaw Imiolek
Jaroslaw Imiolek
6,339 Points

Even or odd challenge, step 3 seems to be broken.

Anytime I click Check Work, communication with server breaks.

even.py
import random
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
start = 5
while True:
    run = random.randint(1, 99)
    if run == even_odd:
        print('{} is even'.format(run))
    else:
        print('{} is odd'.format(run))
    start -= 1

1 Answer

andren
andren
28,335 Points

The connection breaks because your code creates an infinite loop. A while loop will run as long as the condition you provide it evaluates to True, since you are passing it True directly and have no break statements in the loop it will loop indefinitively.

The task asks you to make the loop run until start is falsey. In order to do that you simply need to use the start variable itself as your looping condition. Also even_odd is a function, so you need to call it and pass in the randomly generated number, not compare the number and the function directly with an equality comparison.

If you fix those two issues like this:

import random
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
start = 5
while start: # Test the "truthiness" of start
    run = random.randint(1, 99)
    if even_odd(run): # Call even_odd and pass in the random number
        print('{} is even'.format(run))
    else:
        print('{} is odd'.format(run))
    start -= 1

Then your code will work.