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 trialAshikur Rahman
Python Web Development Techdegree Student 932 PointsCan't solve this step :(
Hi! any one here to help me please. I can't build my login for this part. Your kind help will be appreciated.
Regards
import random
start = 5
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
2 Answers
taejooncho
Courses Plus Student 3,923 PointsYou are really close! You created start = 5 and your while loop inside your even_odd function. You need to create that variable outside your function.
This should get you pass the task
import random
start = 5
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
while start > 0:
num = random.randint(1, 99)
if even_odd(num) == True:
print("{} is even".format(num))
else:
print("{} is odd".format(num))
start -= 1
Rich Zimmerman
24,063 PointsWhen it says "until start is falsey" means when it hits 0. So you want to create a while loop, that calls a random integer, prints whethers it even or odd, and then decrements "start" by 1. You don't want the function to return a value, you just want it to do something, in this case print a string.
import random
start = 5
def even_odd(num):
# If % 2 is 0, the number is even.
# Since 0 is falsey, we have to invert it with not.
# Once start is <= 0, the while loop will be falsey and stop.
while start > 0:
random_int = random.randint(1, 99)
if random_int % 2 == 0:
print("{} is even".format(random_int))
else:
print("{} is odd".format(random_int))
start -= 1
# start is now 4, and the loop will keep running until start == 0
Ashikur Rahman
Python Web Development Techdegree Student 932 PointsSorry, It also can't work. showing erron " Bummer! Wrong number of prints.". anything helpful from you?
Rich Zimmerman
24,063 PointsMy mistake. i forgot the format method
print("{} is even".format(random_int))
# and
print("{} is odd".format(random_int))
updating my answer...
Ashikur Rahman
Python Web Development Techdegree Student 932 PointsBelow is my code with that I'm trying to solve this step. But now it's throwing an indent error message. ridiculous :(
import random
def even_odd():
start = 5
# If % 2 is 0, the number is even.
# Since 0 is falsey, we have to invert it with not.
# Once start is <= 0, the while loop will be falsey and stop.
while start > 0:
random_int = random.randint(1, 99)
if random_int % 2 == 0:
print("{} is even".format(random_int))
else:
print("{} is odd".format(random_int))
start -= 1
# start is now 4, and the loop will keep running until start == 0
even_odd()
Ashikur Rahman
Python Web Development Techdegree Student 932 Pointsstill error "wrong number of print" :(
Ashikur Rahman
Python Web Development Techdegree Student 932 PointsAshikur Rahman
Python Web Development Techdegree Student 932 PointsThe real great your are. Thanks. Really the step challenge description wasn't clear to me and still.
Rich Zimmerman
24,063 PointsRich Zimmerman
24,063 PointsYeah, i made a mistake. Should have tested it first