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 trial

Python Python Basics (2015) Letter Game App Even or Odd Loop

Erik Luo
Erik Luo
3,810 Points

What's wrong?

What's wrong?

even.py
import random
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
start = 5
while start True:
    ran = random.randint(1, 99)
    if even_odd(ran) = 0:
        print("{} is even".format(ran))
    elif even_odd(ran) != 0:
        print("{} is odd".format(ran))
        start -= 1
Cheo R
Cheo R
37,150 Points
while start True:

if even_odd(ran) = 0:
elif even_odd(ran) != 0:

Your while loop is missing a comparison.

In the if statement, you're trying to do assignment, in the elif you're doing a comparison. Fix your while/if statements.

2 Answers

Name:GoogleSearch orJonathan Sum
Name:GoogleSearch orJonathan Sum
5,039 Points
#You have few problems
#first,I want to explain your problem detaily.I hope you can understand it. If you have question ,ask. You can also type @Mr. love to call Mr. love to help you.
#second,you are doing good. When i was doing this code before, i can't even write that a lot as you. 
#you are very close to complete it.


#correct code
import random
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                                # When i was doing this code challenge, i don't know the not statement, it looks like u don't too (explaing 2.5)
start = 5
while start:                            #i will explain why removing True works(explaining1)
    ran = random.randint(1, 99)
    if even_odd(ran) != 0:                            #i switched the == sign to 1=.(explaing2)
        print("{} is even".format(ran))
    elif even_odd(ran) == 0:
        print("{} is odd".format(ran))
    start -= 1                                               #i don't think i need to explain this because ur start -=1 was under the elif statement.

#explaining1
#if u runs it in python 
>>>True ==1
True
>>>True ==5
False   #See, True is 1 ,not 5
>>>start =5
>>>start ==True
False                 #because True is 1 ,not 5. That is why start is not True or equals to True.
#Therefore, the while start True won't work.

#Here comes the HOWEVER

#when u put the start in the if statement or while statement like this ,the start will becomes True,not 5:
>>>if True:
...      print('it is True')
"it is True."
>>> if start:              #it works like if True ,although when u put start ==True, it will give u False. Tag Mr. Love to know more.             
...      print('it is True')
"it is True."
#Therefore 
#it is same when you use "while start" as "while True".
while True
while start

Explain 2

#Your code was:
    if even_odd(ran) = 0:                            #i know u want to say when even_odd(ran) equals zero ,but that is "==" sign. When u r setting like this ,u r asigning variable "even_odd(ran)" to a number zero.
        print("{} is even".format(ran))
    elif even_odd(ran)  != 0:
        print("{} is odd".format(ran))

# after changing = sign to == sign, here is how your code runs (of couse taking out the start -=1 outside of the elif statement.
>>> 
 RESTART: C:/Users/JonathanSum/AppData/Local/Programs/Python/Python35-32/t15.py 
73 is even
23 is even
26 is odd
68 is odd
74 is odd
>>> 

#You see it said 73 is even. 
#That is because u don't understand the "not" statement. I guess Mr. Love didn't explain it too.
>>> 3>2
True
>>> not 3>2
False                #it is just making things oppsite.
#That is why Mr. Love put a comment  like this:
    # If % 2 is 0, the number is even.
    # Since 0 is falsey, we have to invert it with not.
for more detail about how the odd_even function works and why having not statement in even_odd function, plz check the explaining 2.5


#reminder: even your putting a even number in even_odd function, it will give u a True or return  to True.
>>>even_odd(8):
True
>>>even_odd(5):
False
#reminder :True ==1 and False ==0


#Your code again:

    if even_odd(ran) == 0:                       #True is equal to 1 ,not zero
        print("{} is even".format(ran))
    elif even_odd(ran)  != 0:              #Now you understand why ur code gives you odd number is a even? True ==1 and False ==0
        print("{} is odd".format(ran))


Explaining 2.5(Not statement and how even_odd function works:
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

#Not statement,when num =100 and 100%2 =0,. That is why when u put even number into the even_odd function, it will give your zero. However, with the "not" statement, it will convert False or 0 to True.
That is how the even_odd(True_False) function work. When you put even number, it will covert 0 or False to True ,and this True ==1.
Erik Luo
Erik Luo
3,810 Points

But where is the explanation?

Name:GoogleSearch orJonathan Sum
Name:GoogleSearch orJonathan Sum
5,039 Points

i ve spent too much time in explaining it... If u understand what i said ,could u tell Cheo R why adding a comparison , which is == sign i guess, won't work.

#True==1
#True+True=2
#while start ==True is same as while False because the start is not 1.
Erik Luo
Erik Luo
3,810 Points

Thank you so much! I get it now!