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

Doyle Lardell
PRO
Doyle Lardell
Pro Student 888 Points

looks like a glitch but my answer is still wrong

looks like a glitch but my answer is still wrong

not getting it keeps saying task 1 isnt working

even.py
import random
start = 5
def even_odd(num):
    num = random.randint(1,99)
    while start = True:

        if num % 2 = 0
            print("{} is even".format(bobby)
            start -= 1      
        else:
            print("{} is odd".format(bobby)
            start -= 1

    # If % 2 is 0, the number is even.
    # Since 0 is falsey, we have to invert it with not.
    return not num % 2

I think you need to change your operator to equals using == vs. a single = which is just an assignment

i.e.

if num % 2 == 0

Try that, maybe?

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 56,840 Points

The structure of your code got confused. The while loop should not be inside of the is_even function, but rather, it should be built below the function and calls is_even as needed.

In addition to the structure, there were many syntax errors in the above code:

  • error line 5: need to use double-equals ==,
  • error line 7: need to use ==, missing colon end of line
  • error line 8: missing closing paren
  • error line 11: missing closing paren
  • use while start: instead of while start == True:
  • use num instead of the undefined bobby

Post back if you need more help. Good luck!!

Jonathan Beard
Jonathan Beard
1,681 Points

Solution:

Here is what I did

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:
    num = random.randint(1, 99)
    if even_odd(num) is True:
        print("{} is even".format(num))
        start -= 1
    else:
        print("{} is odd".format(num))
        start -= 1