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 trialAlexander Antoshin
1,528 PointsNeed help with the quiz "even_odd"
Hey guys,
Can someone check my code and tell what I'm doing wrong?
import random
start = 5
def even_odd(num):
while True:
if radnom.randint(1, 99) % 2 == 0:
print("{} is even".format(random.randint(1, 99))
elif:
print("{} is odd".format(random.randint(1, 99))
else:
return not num % 2
# If % 2 is 0, the number is even.
# Since 0 is falsey, we have to invert it with not.
start -= 1
1 Answer
Alx Ki
Python Web Development Techdegree Graduate 14,822 PointsHi, Alexander Antoshin !
Well here is the solution.
import random # Import random library
start = 5 # Create start variable
def even_odd(num):
return not num % 2 # This returns True for even and False for odd
while start:
num = random.randint(1, 99) # You must only run random.randint(1, 99) ONCE. Because each time it woulod return a new random.
if even_odd(num): # You check num.
print("{} is even".format(num)) # Print this if function returns True
else:
print("{} is odd".format(num)) # Print this if function returns False
start -= 1
Does it make any sense? If you need more help - just ask!
Alexander Antoshin
1,528 PointsAlexander Antoshin
1,528 PointsHI Alexey,
Fixed the issues but still doens't work. Can you please check the code one more time?
Alx Ki
Python Web Development Techdegree Graduate 14,822 PointsAlx Ki
Python Web Development Techdegree Graduate 14,822 PointsAlexander Antoshin , I updated my answer with solution, also look through the comments.
Alexander Antoshin
1,528 PointsAlexander Antoshin
1,528 PointsIn general I got it but still have some questions:
Alx Ki
Python Web Development Techdegree Graduate 14,822 PointsAlx Ki
Python Web Development Techdegree Graduate 14,822 PointsAlexander Antoshin
Yes
return not num %2
returns a boolean. Since even % 2 == zero or False and odd % 2 == not_zero or True. not just inverts it.Alexander Antoshin
1,528 PointsAlexander Antoshin
1,528 PointsΠ Π°Π·ΠΎΠ±ΡΠ°Π»ΡΡ, ΡΠΏΠ°ΡΠΈΠ±ΠΎ!
Alx Ki
Python Web Development Techdegree Graduate 14,822 PointsAlx Ki
Python Web Development Techdegree Graduate 14,822 PointsAlexander Antoshin ?