Monday, February 28, 2011

Feb 28

Recapping from last week, when i tried to to re-put in the program for print_multiples. I then made an error but when I enter the program I got no error message, so what I did from there was put into the python terminal was print_multiples(3). After I did this, surprisingly I didn't get a error message, I got a continuously flowing 3 table. The program again was:
 def print_multiples(n):
...     i = 1
...     while i <= 6:
...             print m * i, '\t',
...             i += 1
...     print
...
>>>

My modified program of this program was:
>>> def print_multples(n):
...     i = 1
...     while i <= 6:
...             print n * i, '\t',
...     i += 1
... 
>>> 

The error was where i += 1 was suppose to be found right under print n * i, '\t', but instead I missed typed. I tried figuring out a way to allow this program to stop generating the number 3 but so far I've been unable to find a way to get the program to stop the generation. 
def print_mult_table():
i = 1
while i <= 6:
print_multiples(i)
i += 1

The book told me to wrap this program into the program I first created. So the overall program is:
def print_mult_table():
i = 1
while i <= 6:
print_multiples(i)
i += 1
def print_multiples(n):
i = 1
while i <= 6:
print n * i, '\t', 
i += 1
i = 1
while i <= 6:
print_multiples(i)
i += 1

After I applied this program to the terminal I got:
File "print_multiples.py", line 13, in print_multiples
    print_multiples(i)

I got this error a good 50 times in the terminal. I really don't get what the results of this program means. I really don't know if this program has a error or that the program is suppose to out put File "print_multiples.py", line 13, in print_multiples
   print_multiples(i)


Friday, February 25, 2011

Feb 25

So starting with tables, they seem easy pretty easy to understand, well at least the one I've tired. The program is:
x = 1
while x < 13:
        print x, '\t\' , 2**x
       x += 1
This program is basically having calling x to count up to 1-12 on one side of the table('\t\' is keyed together with table). Then on the right side the number starts from 2 then doubles, then doubles, then a that number doubles and so on and so forth.
Then working with the two dimensional table program:

i = 1
>>> while i <= 6:
...     print 2 * i, '  ',
...     i += 1
... print
You get an outcome of the number just counting up by 2s starting with 2 and counting on. Also with this program it tells the number to be spaced 3 times after it is outputted on the table, for example:
2   4   6    8   10   12   14   16   18   20.... and so on.
def print_multiples(n):
...     i = 1
...     while i <= 6:
...             print n * i, '\t\', 
...             i += 1
...     print
When you plug 2 into this program you get:
3   6   9  12  15  18... and so on.
But then when you change the 2 to 4 you get an outcome of:
4   8   12   16   20....and so on. 
The next program combines a few tables to produce a big table:def print_multiples(n):
i = 1
while i <= 6:
print n * i, '\t', 
i += 1
i = 1
while i <= 6:
print_multiples(i)
i += 1
With an outcome of: (sorry took some numbers out so that table look a little equal, side wise) 
1   2   3   4   5   6
2   4   6   8   10  12
3   6   9   12  15  18
4   8   12  16   20
5   10   15   20   25
6   12   18   24   30


Thursday, February 24, 2011

Feb 24

When working with the digit program. I began to think, what if I add more digit space; would it still give me the number of digits in the number. Testing the program:
def num_digits(n):
        count = 0
        while n:
                 count = count + 1
                 n = n /  10
        return count
I then plugged in 9,999,999,999,999,999,999,999. After i enter this number into the program, it correctly came out to have 22 digits.
With working with abbreviation of the numbers i found it really easy to understand that += mean + what ever number. The abbreviation also works with all the other symbols in mathematical expressions such as * / and -. Lets me show you how all the expressions will look. Starting with that count = 0
count = 0
count += 1
count = 1

count = 0
count -= 1
count = -1

count = 0
count += 2
count = 2
count  /= 2
count = 1

count = 0
count *= 1
count = 0

count = 3
count %= 2
count = 1
These are all the expressions you can use.

Tuesday, February 22, 2011

Feb 22

This is the fixed hypotenuse program:

def hypotenuse(a, b):
"""
>>> hypotenuse(3, 4)
5.0
>>> hypotenuse(12, 5)
13.0
>>> hypotenuse(7, 24)
25.0
>>> hypotenuse(9, 12)
15.0

"""
return ((a**2) + (b**2))**0.5

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

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.  

Wednesday, February 16, 2011

Feb 16

This is the fix program of ch05 program:

def compare(a, b):
"""
>>> compare(5, 4)
1
>>> compare(7, 7)
0
>>> compare(2, 3)
-1
>>> compare(2, 1)
1
"""
return a-b

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

this will now pass the doctest
Fix on the hypotenuse program

def hypotenuse(a, b):
"""
>>> hypotenuse(3, 4)
5.0
>>> hypotenuse(12, 5)
13.0
>>> hypotenuse(7, 24)
25.0
>>> hypotenuse(9, 12)
15.0
"""
return

if __name__== '__main__':
import doctest
doctest.testmod()
This is not the completed program yet, im still working on fixing the return so that the program passes the doctest. So far I no that the number needs to have .0 in it to allow the results to have .0 to them. I've ruled out that a**2 + b**2 will not work. I've also tried a - b + 4 which also came out to fail.

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.