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

Pratham Patel
Pratham Patel
4,976 Points

Can I get some help with this

I'm confused on how to make it show that its an even number

even.py
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 == True:
        num = random.randint(1, 99)
        if num == even_odd:
Krahr Chaudhury
Krahr Chaudhury
Courses Plus Student 139 Points

Hi,

I'm no expert on Python, but let me try and help:

Firstly, you're calling the even_odd method incorrectly. It should be:

   if even_odd(num):

I think you need to indent correctly, so the while loop is outside of the even_odd method. (i.e. everything from while should start from the left edge.

I'm not sure what you're trying to achieve in the loop, because start = 5 and not True so the loop won't run. If you wanted to check if it has a value set then you just need to do

while start:

but that would run forever. Is this what you want?

Here's a code that increments start by one after each pass and stops when it's greater than 10. (i.e. runs 5 times)

import random

start = 5

def even_odd(num):
    # another way to return even number
    return num % 2 == 0

while start and start < 10:
  num = random.randint(1, 99)
  if even_odd(num):
    print "%d is an even number" % (num)
  else:
    print "%d is an odd number" % (num)

  start += 1

print "done!"

Another thing, why is the method called even_odd? Is it python practice or shouldn't it be called is_even_number?