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

Nikhil Alexander
Nikhil Alexander
1,444 Points

im cannot seem to correct this error...its saying the code took to long to run

i do not know whether to keep the while True as it or change it according to something in the code challenge

even.py
import random

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

even_odd()   
Ken Alger
Ken Alger
Treehouse Teacher

Nikhil;

Based on the code you posted, can you give me an example of when the while condition would every not be True?

Ken

Nikhil Alexander
Nikhil Alexander
1,444 Points

the while condition would not be true when the number 5 becomes 0

3 Answers

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Nikhil;

Okay, let's back up a minute as it seems you're overall off track a bit, which is fine, this stuff isn't super intuitive.

At the end of Task 2 your code should, more or less, look like this, yes?

import random

start = 5

def even_odd(num):
        return not num % 2

And here are the instructions for Task 3:

Alright, last step but it's a big one. 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 know it's a lot, but I know you can do it!

Let's take those steps one at a time.

  • Make a while loop that runs until start is falsey. Recall that a zero value meets the condition so we can either do while (start > 0): ... or we could simply do while start: ...
import random

start = 5

def even_odd(num):
        return not num % 2

while start:
  • Next we have the code for our number generator and we can assign that to a variable name. num seems as good as any.
import random

start = 5

def even_odd(num):
        return not num % 2

while start:
        num = random.randint(1, 99)
  • If our random number, num is even we want to print that it is. Same for an odd number. We are instructed to use our even_odd method to do so. So we don't need to change that method at all.
import random

start = 5

def even_odd(num):
        return not num % 2

while start:
        num = random.randint(1, 99)
        if even_odd(num):
             // print our even output
        else:
            // print our odd output
  • Finally, we decrement start by 1. That should be doable.
import random

start = 5

def even_odd(num):
        return not num % 2

while start:
        num = random.randint(1, 99)
        if even_odd(num):
             // print our even output
        else:
            // print our odd output
        start -=1

That should do it from a logic standpoint. I'll leave it to you to complete the print statements. ;-)

Post back if you're still stuck, Nikhil. Great job sticking with this as well. You got this!

Ken

Nikhil Alexander
Nikhil Alexander
1,444 Points

Thank you so much for sticking on this task for me and helping me... ive completed the pythons basic course.... now im movin forward... once again thank you Ken. ~NIKX222

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Nikhil;

Actually, the way the above code is written, the loop will never end. You would need to check the value of start to achieve the results you want. Something along the lines of:

start = 5
while (start > 0):
    ...
    start -= 1

Doing while True: runs forever unless there is a break statement.

Ken

Nikhil Alexander
Nikhil Alexander
1,444 Points

thank you ken, i understand that part...... but when i write (start > 0)....it says task one is no longer passing... i know im making some mistake.. could you help me find and and correct that??

Ken Alger
Ken Alger
Treehouse Teacher

Can you please post the most current version of the code your using in this challenge?

Thanks, Ken

Nikhil Alexander
Nikhil Alexander
1,444 Points
import random

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

even_odd()

I even tried using

while (int(start) > 0):
Ken Alger
Ken Alger
Treehouse Teacher

Doh! I just noticed that you are defining your even_odd function inside the while loop.

I would define even_odd() separately:

def even_odd(num):
        return not num %2

And then call that inside your while loop. Sorry for the confusion on that.

Ken

Nikhil Alexander
Nikhil Alexander
1,444 Points

thank you for helping me... but im sorry to bother you...the code still doesnt work could you help me out??

import random

start = 5

def even_odd(num):
        num = random.randint(1, 99)
        if num % 2 == 0:
            print("{} is even".format(num))
        else:
            print("{} is odd".format(num))
        return not num % 2

while (start > 0):
    even_odd(num)
    start -= 1