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

Jeetender Batra
PLUS
Jeetender Batra
Courses Plus Student 3,028 Points

Make a while loop that runs until start is false. Need explaination while start>1: ?

Need explanation

even.py
import random

start=5

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

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

4 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,426 Points

At the start of each while iteration, the condition start > 0 is checked. if the value of the start variable is greater than zero, another pass through the block of code is performed.

In the code posted, start has an initial value of 5, so the while loop will execute. At the end of the code block, the statement start +=1 will increment start to be 6. As you can see, the value of start keeps increasing and will never drop below 0. Hence, the while loop will never end.

The last while block statement should decrement start so that after 5 iterations, it will be 0. Try changing it to read start -= 1

Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

One other error in the posted code is the function even_odd is referenced before being defined. The while loop code must come after the definition of the even_odd function.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

Bugs are rare in Treehouse challenges. Post back if you're still having issues with it.

Jeetender Batra
PLUS
Jeetender Batra
Courses Plus Student 3,028 Points

Yep, I have sent an email to support team for help with screenshot. Thank you Chris..!

Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

I don't see the error. Can you be more specific on what you think the bug is?

Jeetender Batra
PLUS
Jeetender Batra
Courses Plus Student 3,028 Points

Hello Chris,

Actually the message shown on the challenge was in correct. problem was i have defined function below while loop. function definition should come first.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,426 Points

Yes. The error messages returned by the challenges can be vague or general. From what I understand, the issue is a reduced granularity in the information passed to the checker on some code failures within the challenges. The Challenge checker code then has to make the best of what is reported. This sometimes leads to the generic "Bummer" message when a more detailed message would have been more helpful.