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

Isaac Barron
Isaac Barron
7,011 Points

On task 3 of the final part of Python basics I can't figure out what I'm doing wrong. I know its something simple.

Challenge Task 3 of 3

Alright, last step but it's a big one. Make a while loop that runs until start is falsey. Inside the loop, use random.randint(1, 99) to get a random number between 1 and 99. If that random number is even (use even_odd to find out), print "{} is even", putting the random number in the hole. Otherwise, print "{} is odd", again using the random number. Finally, decrement start by 1. I know it's a lot, but I know you can do it!

ERROR => Bummer! Try again!

Error message isn't giving much to go off of and I copied and pasted this some code somewhere else and it worked fine

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:
  num = random.randint(1, 99)

  if even_odd(num):
    print('{} is even').format(num)
  else:
    print('{} is odd').format(num)

  start -=1

3 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,441 Points

The error is in the syntax. format() is a string method and must be used with a string. You have parens between the string and the method.

  if even_odd(num):
    print('{} is even'.format(num))
  else:
    print('{} is odd'.format(num))

Chris, I'm having a bit of difficulties with this one.

I can't seem to understand the logic behind the code.. I'm a bit confused by the meaning of these lines:

return not num % 2

and this section:

start = 5

while start:
  num = random.randint(1,99)  
  if even_odd(num):

Could you translate them for me in 'English'?

Thank you and happy new year!

Mo.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

There is a little bit of wizardry going on in return not num % 2

num % 2 is modulo math that divides a number until there is only a remainder. using % 2 results in a 0 for even numbers and a 1 for odd numbers.

If looking at the "truthiness" of this result, an even number would get 0 or False, and an odd number would get 1 or True. This is the opposite of what we want.

Using a not reverses the results so not False (for even numbers) becomes True and not True (for odd numbers) becomes False.

Therefore, return not num % 2 returns True for even numbers and False for odd numbers

In the next section of code, the while loop is using the "truthiness" of start to decide to loop. As long as start isn't 0 ("falsey") then the loop continues.

# Set start to run loop 5 times
start = 5

# While start is not 0, run loop
while start:
  # Generate a random integer between 1 and 99
  num = random.randint(1,99)
  # Use even_odd function to determine if num is even (True returned) or odd (False returned)
  if even_odd(num):
      # ... Action if even
  # decrement start to count down the loops
  start -= 1

Note that the helper function even_odd() would be better named as is_even() so that the if statement makes more sense as:

  if is_even(num):
      # ... Action if even

One warning on using simple "truthiness" on a variable. If start got decremented by 2 (due to a bug) the value 0 would be missed when start jumped from 1 to -1. Negative numbers are "True" and the while loop would run forever.

Thanks for the explanation Chris!

I am getting an error that states my "code took too long to run."

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

def start():
  start = 5

while start:
  num = random.randint(1, 99)
if even_odd(num):
  print('{} is even'.format(num))
else:
  print('{} is odd'.format(num))
start -=1
Chris Freeman
Chris Freeman
Treehouse Moderator 68,441 Points

Scott, your indentation is off. The last five line need indented on stop:

while start:
  num = random.randint(1, 99)
  if even_odd(num):
    print('{} is even'.format(num))
  else:
    print('{} is odd'.format(num))
  start -=1

Without the indent changes the only statement in the while loop. is the num assignment. start never changes and the loop runs forever (or until the cpu cap is hit)

Oops. Thanks Chris.

Isaac Barron
Isaac Barron
7,011 Points

O ok thanks. Very odd that it worked as expected elsewhere.

Thank you for your Q&A