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

Duarte Reis
seal-mask
.a{fill-rule:evenodd;}techdegree
Duarte Reis
Python Web Development Techdegree Student 897 Points

Do not understand the meaning of some code

As I was stucj in this problem, I came across this code online that worked:

I have some question:

1 - What the purpose of the function even_odd (num): ? what does it return?

2- Just "while start:"? why not "while start !: 0"?

3- when we have the code "if even_odd(num):" what does it mean? What am i checking with this condition? If it returned not a number with remaining 0?!

Thanks a lot in advance.

Duarte

even.py
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:
        num = random.randint(1,99)
        if even_odd(num):
            print("{} is even".format(num))
        else:
            print("{} is odd".format(num))
    start -= 1

3 Answers

Ryan S
Ryan S
27,276 Points

Hi Duarte,

1) The "%" operator in Python will divide a number by a number and return the remainder. Note that this is not the decimal number. It is referring to the leftover whole numbers after division.

So for example:

3 / 2 = 1.5
3 % 2 = 1  #  2 x 1 = 2, plus 1

11 / 4 = 2.75
11 % 4 = 3   # 4 x 2 = 8, plus 3

4 / 2 = 2
4 % 2 = 0  #  2 x 2 = 4, plus 0

So in the above example you can see that when you have an even number, if you "% 2", you will always get zero. The issue is that 0 is also False. So in the even_odd function, if you don't include the "not" keyword, it will return False every time a number is even. This is the opposite of what we want. In order to correct this inversion you'd use "not" which basically turns the statement into "return not False"

2) and 3). When you use conditions like while start: and if even_odd(num), you are checking whether the conditions are True. It is just a cleaner way of typing it.

You could use the following instead:

while start != 0:
# or
while start != False:
# or
while start:

if even_odd(num) == True:
# or
if even_odd(num):

Hope this clears things up.

andren
andren
28,558 Points
  1. The purpose of the function is to determine if a passed in number is even or odd, it will return either the boolean value True (number is even) or False (number is odd) I get more into how it does so below.

  2. Because that would be redundant due to the fact that in Python some values are considered inherently "truthy" and inherently "falsey". There are a number of such values but for this example all you really need to understand is that as far as Python is concerned 0 is a "false" value, meaning that if it is used in a place where a boolean would normally go then Python will treat it as False, the opposite is true for any number other than 0, those are considered "true". So while "start" is a number other than 0 it will be treated as being True, once it becomes 0 it is treated as being False.

  3. In the even_odd function it calculates the remainder of the passed in value and 2, and then it inverts the truthiness of whatever value came as a result by using the "not" keyword, so if the result is 0 (which again is considered to be False) then the "not" turns it into True, and if the value is anything but 0 the "not" turns it from True to False. So ultimately what is returned is just a True or False boolean, as mentioned above in point one.

Duarte Reis
seal-mask
.a{fill-rule:evenodd;}techdegree
Duarte Reis
Python Web Development Techdegree Student 897 Points

Hey guys. Thanks a lot for taking the time to reply.

It is clear now! Sometimes the videos make a huge step ahead and I don't understand some of the basic concepts yet.

Best regards!