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 trialTatiana Brittingham
3,095 Pointscode runs fine on bash but doesn't pass task 1 here
cant seem to figure out the error for task 1 failing - works on it own
import random
def even_odd(num):
if num % 2 != 0:
return not num % 2
start = 5
while start:
num = random.randint(1,99)
if even_odd(num) is False:
print("{} is even").format(num)
else:
print("{} is odd").format(num)
start -= 1
3 Answers
Steven Parker
231,269 PointsThe original function provided by the challenge has no conditional (shown here without the comments):
def even_odd(num):
return not num % 2
So it always returns a boolean, "True" for even numbers and "False" for odd ones.
Then you can use it as-is in the test, without comparing it to anything:
if even_odd(num):
Hope I was able to help! You can mark the question solved by choosing a "best answer".
And happy coding!
Steven Parker
231,269 PointsPerhaps the code here is a bit different from what you tested with. Here, the "format" method is being applied to the result of calling "print" on the template string. You probably want to apply the format directly to the template string and then print the result.
Also, the challenge will not expect you to change the supplied "even_odd" function.
Finally, if you compare the result of "even_odd" to "False", it will invert the result (for example, it will claim "22" is "odd"). And since "even_odd" returns a boolean, it doesn't need to be compared with anything and can be tested directly.
Tatiana Brittingham
3,095 Pointsthank you - i assigned a str.format() value to odd - even vars, then print(odd), print(even), and then it worked!
the original code did work on bash - here are test results:
15 is odd 89 is odd 13 is odd 70 is even 51 is odd
from my understanding, the even_odd function tells the computer to not return the remainder, if the given number is odd, which sets the return value to False bool. Therefore, if i check whether my function returns False (if even_odd(num) is False: ) - my num is odd, which makes the original code work. what do you think ? it is not necessary, i modified a lot of times to push it through here. thank you again!