Tuesday, February 15, 2011

Feb 14-15

Fix on #4:

def is_even(n):
"""
>>> is_even(1)
False
>>> is_even(2)
True
>>> is_even(3)
False
>>> is_even(4)
True
>>> is_even(5)
False
>>> is_even(6)
True
>>> is_even(7)
False
>>> is_even(8)
True
>>> is_even(9)
False
"""
return n % 2 == 0
if __name__== '__main__':
import doctest
doctest.testmod()

and which now will allow for the program to run and pass. 
Fix on #5:
def is_odd(n):
"""
>>> is_odd(1)
True
>>> is_odd(2)
False
>>> is_odd(3)
True
>>> is_odd(100)
False
>>> is_odd(9999)
True
>>> is_odd(1000000)
False
>>> is_odd(300957)
True
>>> is_odd(298745230)
False
"""
return not n % 2 == 0
if __name__== '__main__':
import doctest
doctest.testmod()
This then allows the program to change from having to check to see if the number is even to check to see if the number is odd. What I had to change from the is_even program was the return n % 2 == 0 to return not n % 2 == 0. Which this is meaning that if the number(n) is divisible by 2 and has nothing left over, it is not a odd number, but sense for the is_odd program I applied not to the body to tell that if the numbers do not fight the divisible property of 2 = 0 then it will be a odd number. 
There was not to much put on for these too days due to I was trying to figure out what I had to do to change the output of the is_even program so that it outputs the right figures for is_odd program.

1 comment:

  1. Nice! using not (n % 2 == 0) for odd show good understanding of boolean operators.

    ReplyDelete