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 trialmarinmirosevic
3,279 Pointshi...need help with this code. thanks
hi...need help with this code. thanks
import random
secret_num=random.radnint(1,10)
while True:
even_odd=int(input("Put a number"))
if even_odd==secret_num:
print("YouGoIt MyNumberwas{}".format(secret_num))
break
else:
even_odd_1=even_odd/2
if int(even_odd_1)=even_odd_1:
print("Number is even")
else:
print("Number is not even")
6 Answers
Steven Parker
231,275 PointsIt looks like you may have copied from a different challenge (or two!).
You might want to read the challenge instructions again carefully. Some of what it asks for is very different from what you wrote:
- the challenge asks you to write a function
- your function will require and test a passed argument
- it will not need to input anything
- it will not need any random value
- the function will return the result
- the results will only be True and False
- it will not need to print anything
Try again, and be careful to do only what the instructions ask for.
vikas pal
11,563 Pointsyour randint spelling is wrong in line 2
random.randint(1,10)
Steven Parker
231,275 PointsKnowing the correct spelling of randint might be useful in the future, but it will not help this challenge.
marinmirosevic
3,279 Pointsthanks Steven
does this looks better?
def even_odd(number): number=6 number_1=6/2 if int(number_1)==number_1: True else: False
Robin *
5,670 PointsThere is no need to define the number inside the function. The number is passed to the function when it is called for example: even_odd(3) where 3 is the 'num'
You should use a modulo operation in order to figure out if the number has a rest value or not. In python % is the operator for a modulo operation. for example: 5 % 2 = 1 It is 1 because 5 divided by 2 leaves a quotient of 2 and a remainder of 1.
If you are still reading this then you might have realized that if you take an odd number and MOD by 2 you will get a value that is "Truthy" and if you take an even number and MOD by 2 you will get a value that is "Falsy". So our even_odd function we have to take that in to consideration.
We can achieve this by using the not operator.
def even_odd(num):
return not num % 2
We could also do it like this
def even_odd(num):
if num % 2 == 0:
return True
else:
return False
marinmirosevic
3,279 Pointsthanks
Windy Elliott
776 PointsThank you all!!