Friday, February 18, 2011

Feb 17- 18

This is a fix on the factor program:

def is_factor(f, n):
"""
>>> is_factor(3, 12)
True
>>> is_factor(5, 12)
False
>>> is_factor(7, 14)
True
>>> is_factor(2, 14)
True
>>> is_factor(7, 15)
False
"""
return n % f == 0
if __name__ == '__main__':
import doctest
doctest.testmod()

This is a fix on the is divisible by 2 or 5 program:
def is_divisible_by_2_or_5(n):
    """
      >>> is_divisible_by_2_or_5(8)
      True
      >>> is_divisible_by_2_or_5(7)
      False
      >>> is_divisible_by_2_or_5(5)
      True
      >>> is_divisible_by_2_or_5(9)
      False
    """
    return n % 2 == 0 or n % 5 == 0

if __name__ == '__main__':
    import doctest
    doctest.testmod()

This is a fix on the multiple program:
def is_multiple(m, n):
    """
      >>> is_multiple(12, 3)
      True
      >>> is_multiple(12, 4)
      True
      >>> is_multiple(12, 5)
      False
      >>> is_multiple(12, 6)
      True
      >>> is_multiple(12, 7)
      False
    """
    return m % n == 0
if __name__== '__main__':
import doctest
doctest.testmod()

Chp 6:
When starting out using multiple assignments, I noticed that when you first put in a number like lets say a = 3 and then u change it to where a = 4. When you tell python to print a you'll get 4 instead of 3 because sense you changed a value to being 4 last, that would be the output of the number. Also when you try to assign the variable a number you must always have the variable going first before the number. Ex: a = 7 correct, 7 = a incorrect. When you put 7 = a, that is illegal meaning it will not work. 
On updating variables, I've found out that when you first put a variable in like x = 99 then if u want to improve the variable to where its a different number you can just do x = a different number but you can also use x = x -/+* a number(you can use all equation symbols). So lets say I wanted to change x = 99 to x = 100, so what I would do is ether do x = 100 or i can do x = x + 1. After putting the second x = x + 1 and you say print x, you'll get 100. 
Using the countdown program helped me understand that when you use a variable like n and then put n > 0:   print n then n = n - 1. When you put n = n -1 and n > 0, this basically will continue a count down until it hits 0 then will print or do what ever you tell it to print.  

No comments:

Post a Comment