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

or porat
or porat
684 Points

Even_odd problem

Hey! I tried to check when even_odd is even, Did i make it right? second thing, i used '.format()' to fill the hole of the random number but what should i write inside the '.format()' ? third thing, why to decrement 'start' by -1?

and if my code should look different ill be glad to get some help! thanx and happy new year!

even.py
import random

start = 5

while:
    random.randint(1,99)
    if even_odd = % 2
        print("{} is even".format(random.randint))
    else:
        print("{} is odd".format(random.randint))
        start -1
    is start = False:
        break


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

I would encourage you to use the workspace provided if you are continually failing on one of the challenges. I had difficulty on this one earlier today.

The challenge asks you to use the even_odd function. You are not using it here. In python function definitions need to be at the top of the file. For example the following is incorrect.

test(123)

def test(number):
  print(number)

The above code will error because the test function is not defined when it is being called. However, the following code will work.

def test(number):
  print(number)

test(123)

Starting at the top of what you have done. Put the even_odd function at the top of the file.

You then want to assign the value of the random.randint() call to a variable.

random_number = random.randint(1,99)

Once you have done this, in your while loop, you can check to see if the number is even.

if even_odd(random_number):

Your messages can then be printed with your variable.

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

In your code you have written: start -1. All this is doing is assigning the value of start - 1 to nothing. Instead you want to have.

start -= 1

This will reduce the start variable by 1 on each iteration of the loop.

Finally on line 12 you should have if. You wrote 'is'.

Once you put all of it together I would expect you to be able to pass the test. If not again, I encourage you fire up the work environment. Put your code in a file and then run the file. When you see what it is doing you will probably work out how to fix it.

1 Answer

Steven Parker
Steven Parker
231,248 Points

In addition to Jason's advice, here's the answers to your questions:

I tried to check when even_odd is even, Did i make it right?
You have a few syntax errors, see Jason's comments for details

second ... what should i write inside the '.format()' ?
You should pass the variable where you store the random number to the format()

third thing, why to decrement 'start' by -1?
The start variable acts as a loop counter. You decrement it so the loop will stop after running 5 times.

And for that loop counter to work, you'll need to test the value of start in your while statement:

while start:

When the value of start reaches 0, it will be considered "falsey" and the loop will end.