Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Letter Game App Even or Odd Loop

Nicholas Anstis
Nicholas Anstis
2,095 Points

Python Basics excersise

Here's that question :

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 can't figure it out by myself i need help from you guys

even.py
import random
secret = random.randint(1, 99)
start = 5


while False:

  guess = int

def even_odd(num):
  if guess == secret:
    print("{} is even".format(secret)
  else:
          print("{} is odd".format(secret)


    # If % 2 is 0, the number is even.
    # Since 0 is falsey, we have to invert it with not.
    return not num % 2
else:
  if start == False:
    break

2 Answers

Steven Parker
Steven Parker
216,017 Points

Here's a few hints:

  • don't change the even_odd function that was provided.
  • put your while loop at the bottom of the file
  • make sure your while is checking the value of start
  • create the random number inside the loop
  • make use of even_odd in an if statement to test the random number
  • after your if/else and prints, remember to reduce start

I'll bet you can get it now without an explicit spoiler. But post again if you need more help.

Nicholas Anstis
Nicholas Anstis
2,095 Points

Ok thanks a lot. Ill try to figure that out :D !