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

Amber Diehl
Amber Diehl
15,402 Points

quiz returning wrong number of prints when clearly there's two prints specified in the task

Taking Python Basics quiz where random number is passed to even_odd and need to print odd or even message based on result. Getting error message about wrong number of prints but task clearly states print the result in the loop.

start = 5

while start == True: random_number = random.randint(1, 99) if even_odd(random_number) == 0: print("{} is even".format(random_number)) else: print("{} is odd".format(random_number))

start -= 1

even.py
import random

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

start = 5

while start == True:
  random_number = random.randint(1, 99)
  if even_odd(random_number) == 0:
      print("{} is even".format(random_number))
  else:
      print("{} is odd".format(random_number))

  start -= 1

2 Answers

2 different print statements are specified and you have them both. The error you are receiving corresponds to those statements being used in your while loop. Your placeholder, start has a value of 5 at the initiation of your while and is reduced by 1 every time the while loop gets to its end. Your while runs over and over again until start hits a "Falsey" state.

Imagine that random_number has a value of 2 and you pass it into even_odd to check 2's even or oddness. Look hard at even_odd's return.

  1. Is the result of even_odd(random_number) going to == 0 ?
  2. Will the result of even_odd(random_number) require a comparison to its own value?
  3. Can the same principle of redundancy be applied to the while line?

Let me know if you need any more help!

Amber Diehl
Amber Diehl
15,402 Points

Thank you for your answer!! I actually figured out it was the == 0 and why. But thank you again for your help!!

Exactly!

while start:

will return True as long as start has not reached 0 and...

if even_odd(random_number):

...will return True if the number is even because the not in your...

return not num % 2

...inverts the falseness of the 0 from the number being even.