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 trialKyle Butterworth
5,743 PointsIssue with modulo (%) operator in coding challenges?
I'm trying to complete a coding challenge and it says there is an error somewhere with this code:
import random
def even_odd(num): boo = False if num % 2 == 0: boo = True return boo
start = 5
while start: rand = random.randint(1, 99)
if even_odd(rand):
print("{} is even").format(rand)
else:
print("{} is odd").format(rand)
start -= 1
import random
def even_odd(num):
boo = False
if num % 2 == 0:
boo = True
return boo
start = 5
while start:
rand = random.randint(1, 99)
if even_odd(rand):
print("{} is even").format(rand)
else:
print("{} is odd").format(rand)
start -= 1
2 Answers
Sean T. Unwin
28,690 Pointsif num % 2 == 0:
does not need to be evaluated against a 0
as the operation, num % 2
returns True
or False
only.
Steve Hunter
57,712 PointsHi Kyle,
There's a couple of points to deal with here. First, don't mess with even_odd()
; it works just fine. You send it a number and it returns True
or False
depending whether the number is even or not. So, put that back as it was at the start of the challenge.
Then, you've not chained .format()
onto the strings. The method is outside the brackets. Try moving the .format()
inside the brackets of the print
statement:
print("{} is even".format(rand))
That should work, then.
Let me know how you get on.
Steve.