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

even_odd function

am getting a 'Wrong number of prints ' message after writing the attached code and am stuck
import random
start = 5
while start < 5:
        secret_num = random.randint(1, 99)

        if even_odd(secret_num):
            print("{} is even.").format(secret_num)
            print("{} is odd.").format(secret_num)
        start -= 1

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

2 Answers

Your print statement has an error

print("{} is even.").format(secret_num)

The format method is called on the String as show below:

print("{} is even.".format(secret_num))
Alex Koumparos
Alex Koumparos
Python Web Development Techdegree Student 35,917 Points

Hi Clainos,

Your problem is in the loop definition.

Your code says:

start = 5
while start < 5:

Since you made start 5 and your program only enters the loop if start is less than 5, your loop will never execute.

Note that the task tells you to "Make a while loop that runs until start is falsey." This is telling you that your loop should run until start has a value that Python considers False. We know that start is an integer, so what types of integer are False? You can check this yourself in the Python interpreter by typing something like:

5 == True

The interpreter will come back and tell you that is True. Similarly:

1 == True

Interpreter will again tell you that is True. What about:

0 == True

This time, Python will tell you that is False. So we know that 0 is a falsey value.

The body of your while loop decrements start at the end of every iteration, so your value of start will reach 0, at which time it will be False.

Therefore, you can set your while loop to run until start is False. This is as simple as writing:

while start:

Hope that clears everything up.



changed that and now getting the message 'Task one is no longer passing'