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 trialMadeline Yao
Full Stack JavaScript Techdegree Student 9,611 PointsHow 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?
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
1,979 Pointsimport 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
27,276 PointsHi Madeline
Here are the major points of the challenge:
- 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. - The while loop in task 3 needs to be outside of
even_odd
. In this while loop, you will calleven_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 useeven_odd
in theif
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
1,979 PointsHey 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
27,276 PointsHey 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!