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

Madeline Yao
seal-mask
.a{fill-rule:evenodd;}techdegree
Madeline Yao
Full Stack JavaScript Techdegree Student 9,611 Points

How to fix the code?

Hello everyone, I tried to finish the code challenge and I did not make it right but I do not know how to fix it. Could anyone help me with the fixation?

even.py
import random
def even_odd(num):

    while start > 0: 
        num = random.randint(1, 99) 
        if num % 2 = 0: 
            print("{} is even".format(num))
        else: 
            print("{} is odd".format(num))
        start-= 1
    # If % 2 is 0, the number is even.
    # Since 0 is falsey, we have to invert it with not.
    return not num %2
start = 5 

2 Answers

Hassaan Ahmad
Hassaan Ahmad
1,979 Points
import random
def even_odd():
    start = 5
    while start > 0: 
        num = random.randint(1, 99) 
        if num % 2 == 0: 
            print("{} is even".format(num))
        else: 
            print("{} is odd".format(num))
        start-= 1
    # If % 2 is 0, the number is even.
    # Since 0 is falsey, we have to invert it with not.
    return not num %2


even_odd()

I have corrected this code as per my knowledge and it works well.

There were the following problems with your code:

  • The defined function wasn't called in the main body of your code.
  • 'start' variable had to defined in the function or should have been fed to the function using a parameter. I used the former.
  • The parameter (random.randint(1, 99)) could have been fed to the function but i preferred to make the function such that it didn't require any parameters to initiate.

Hope this helps.

Ryan S
Ryan S
27,276 Points

Hi Madeline

Here are the major points of the challenge:

  1. Do not modify the even_odd function. The function is already written for you and you are not asked to change it in any way. What you are asked to do, though, is use it to determine whether a number is even or odd. The function is already set up to do that. It returns True if the number is even, and False if it is odd.
  2. The while loop in task 3 needs to be outside of even_odd. In this while loop, you will call even_odd in order to check if a number is even or odd. The logic that you have in the loop is definitely on the right track, but you will need to use even_odd in the if condition, instead of "num % 2 == 0".

While Hassaan's code does pass the challenge, I think it is important to point out that it does not actually meet the specific requirements of what the challenge was asking. There is a big difference between modifying a function and modifying the code that calls the function, even if the results seem to be the same for a specific situation.

I only bring this up because correctly determining the constraints of a problem can be just as important as arriving at a solution. In practice, functions can be used many times over throughout a project and if you happen to modify one that you shouldn't, it could cause a lot of problems in other areas of the code.

The code for this challenge should end up taking the following format:

import random

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

start = 5

while start:
    # write your code here

Hope this helps.

Hassaan Ahmad
Hassaan Ahmad
1,979 Points

Hey Ryan, I made a mistake by not checking out the challenge and just correcting his code in my own way. Thanks for pointing that out, really appreciated.

Ryan S
Ryan S
27,276 Points

Hey no problem. By the way, you are the first person I've seen that was able to pass this one by modifying the function. That is impressive in itself, especially if you didn't even check out the challenge!