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

Daniel McFarlin
Daniel McFarlin
5,168 Points

Really struggling on this one, can't seem to wrap my head around it help please

okay so I'm pretty sure I am off at a few parts here, but the last part especially in the question the part about "decrement start by 1." Not sure where to start on that one. Any help that could be given will be much appreciated! Thank you!

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



# If % 2 is 0, the number is even.
# Since 0 is falsey, we have to invert it with not.

2 Answers

Taylor Schimek
Taylor Schimek
19,318 Points

Well, there are several things going on here:

  1. the while loop doesn't go inside the even_odd function. Instead, you use even_odd in the while loop to find out if num is even or odd.

  2. the num = randomin(1, 99) should read num = random.randint(1, 99). also, this line should be in the while loop so that you get a new num each time it loops

  3. while True: should be something like while start > 0: Zero is falsy; anything above it is truthy. start is 5 to begin with. Inside the while loop, decrement start like this: start -= 1. When it hits 0, it'll be falsy and stop the loop.

hope that helps.

Daniel McFarlin
Daniel McFarlin
5,168 Points

Sure did. Turns out all I needed to do was walk away from the computer and come back fresh. It clicked after doing that. I guess my brain was just fried! Thanks for giving me a few pointers though!