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

Dezhi Zhu
Dezhi Zhu
2,662 Points

I need some help please

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.

even.py
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:
    random_number = random.randiant(1,99)
    if even_odd(random_number):
        print("{} is even".format(num))
    else:
        print("{} is odd".format(num))




    start = start - 1

1 Answer

Ryan S
Ryan S
27,276 Points

Hi Dezhi,

Your logic is on the right track, but there are two issues with your code.

The first is a typo when calling "random.randint()". You have an extra "a" in there.

The other issue is with your .format() method calls in your print statements. You are trying to format the string with a variable named "num", however you have not defined "num" anywhere in your loop. Remember that you don't have access to the function parameters outside of the function itself. You can only utilize the return value of the function.

Dezhi Zhu
Dezhi Zhu
2,662 Points

Hi Ryan I tried the following code. But it still not works. Could you please tell me why? I appreciate for your help.

regards Dezhi

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:

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


start = start -1
Dezhi Zhu
Dezhi Zhu
2,662 Points

Sorry Ryan I realized it is again a misspelling error. It is so annoying! Is there anyway to detect that error in Python?

regards Dezhi

Ryan S
Ryan S
27,276 Points

Hi Dezhi,

Nice catch on the typo. Normally, yes, Python would inform you of a typo. If you were running this outside of a challenge, you'd most likely get a "NameError: name 'randon_number' is not defined", along with the line number of where the error occurred.

The challenges are not always as informative and it can make things really tricky to debug. I would say that some of the challenges are often a little harder than they would be outside of the Treehouse environment simply because of the lack information regarding errors.

The best option would be to try and run the code in workspaces or on your local machine. But even with this you have to be careful because your code may run perfectly fine without any errors in workspaces, but you still have to make sure you are doing exactly what the challenge asks.