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

Laknath Gunathilake
Laknath Gunathilake
1,860 Points

unable to pass the challenge

I don't understand the part "Finally, decrement start by 1"

Also, I'm not sure, if the first part of the code I've written is correct

even.py
import random
start=5
def even_odd(num):
  while start=True:
    num=random.randint(1,99)
    if num %2==0:
      ("{} is even".format(number))
    else:
      ("{} is odd".format(number))

    # 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

You are off to a good start.

  1. You have random imported

  2. You gave the start variable a value of 5

Now:

  1. You have the pre-made function even_odd that takes an argument num and returns: not num % 2

    You don't need to edit this function. It is important to look at what this function is doing. As it is, even_odd will take num and return a value based on the rules that were given "# If % 2 is 0, the number is even. # Since 0 is falsey, we have to invert it with not." Take a moment to ponder exactly what value will be returned if num were equal to 2 or if num were equal to 3. This function becomes useful later.

  2. "Make a while loop that runs until start is falsey." You have already given start a value of 5, making start inherently True. This loop is based on 2 ideas. 1: The loop having the condition of while. 2: The truthiness of start. You have the while but your check of start against the value of True is excessive because start is inherently True. You might think of it as asking: While start is equal to True, (while start==True:) but in logic, this is redundant because: start is True. All you really need are while and start.

  3. You use random.randint(1, 99) inside your while loop. to get your random number.

  4. Use the even_odd function to check if your random number is even or odd. When you pass your random number to even_odd, imagine that function call replaced with the value it will return. Just like step 4, avoid redundancy in your if statement.

  5. Both your print statements are missing something very important and both have something undefined. I bet you can figure out what they are with a little review.

  6. After all of these things in your while loop, the value of start should be decremented (minus equal) by 1.

So... start is 5 (making it True). while start:. You get a random number and check if it is even using your pre-made even_odd function. You print the first statement if the random number is even, else, you print the second statement. You reduce the value of start, making it 4. The loop repeats. And again. And again. And again. Now, you will have printed 5 statements, start will be decremented, recursively, to 0 and the while loop will end.

See how far this gets you and check back in with me if you are still stuck.

Laknath Gunathilake
Laknath Gunathilake
1,860 Points
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.

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


Now it says task one is no longer passing

I think I understand number 1,2 and 3, but really clear on 4 5  and 6

You're closer! Try leaving the even_odd function and your while loop independent of each other. There is no need to nest one in the other when you can just call even_odd using your random number from your while loop.

Laknath Gunathilake
Laknath Gunathilake
1,860 Points
import random
start=5
def even_odd(num):
num=random.randint(1, 99)
while start:
  if num return not num % 2
    print ("{} is even".format(num))
  else:
    print ("{} is odd".format(num))
  start -=1
,,,

is this what you meant by making the two independent. It still say's task 1 not passing

Without giving you the answer, let me see if I can get you pointed in the right direction. The even_odd() function is fine the way that it comes.

def even_odd(num):
    return not num % 2

while start:
    num=random.randint(1, 99)
    if #FILL IN THE BLANK:
        print ("{} is even".format(num))
    else:
        print ("{} is odd".format(num))
    start -=1
Laknath Gunathilake
Laknath Gunathilake
1,860 Points

so its

if num%2=='0':

I was able to pass the challenge. Thank you

if num % 2 == 0: doesn't use your even_odd() function, which is a specification of the exercise.

Allison Hanna
Allison Hanna
36,222 Points

So that the loop will eventually reach a falsey value (in this case, 0) and stop running, decrement the value of the start variable by 1 each time. So inside your loop, after your if/else blocks,

start -= 1

Otherwise, start will always returns true and your'e stuck in an infinite loop.