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.

Friday, February 11, 2011

Feb 11

OK when working on questions #4 and #5, I've been having some difficulty with it. What I'm suppose 2 do is for question #4 is make a program that determines if a number is even and if it is even then it will out put True. If the number is an odd number then the out put would come out to be False. Then see for question #5 I have to do the same thing but instead for only odd numbers. Ill continue on this program and see how far I can get.
So far this hasn't given me any problem or said that the test has failed for question #4 so this is the program:

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
"""
if __name__== ' __main__':
import doctest
doctest.testmod()

#5: def is_odd(n):
"""
>>> is_even(1)
True
>>> is_even(2)
False
>>> is_even(3)
True

>>> is_even(4)
False

>>> is_even(5)
True

>>> is_even(6)
False

>>> is_even(7)
True

>>> is_even(8)
False

>>> is_even(9)
True

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

Thursday, February 10, 2011

Feb 10

********************************************************************************
File "intercept.py", line 5, in __main__.intercept
Failed example
      intercept(6, 1, 1, 6)
Expected:
      7.0
Got nothing
*******************************************************************************
File "intercept.py", line 7, in __main__.intercept
Failed example:
      intercept(4, 6, 12, 8)
Expected:
       5.0
Got nothing
*******************************************************************************
1 items had failures:
     3 of     3 in __main__.intercept
***Test Failed*** 3 failures.
#4:

Wednesday, February 9, 2011

Feb 7-9

Alright on Monday I wasn't able to get on due to computer issues. This continued to at least half of the class on Tuesday. So today i'll continue on Chapter 5 questions #1.
#1: def compare(a, b)
"""
>>> compare(5, 4)
1
>>> compare(7, 7)
0
>>> compare(2, 3)
-1
>>> compare(42, 1)
1
"""


"""
>>>compare(a > b)
1
>>>compare(a == b)
0
>>>compare(a < b)
-1
"""


if __name__== ' __main__':
import doctest
doctest.testmod()
This program really got me for awhile. When I first started out I had changed the def compare(a, b) to def compare( a > b). Which was giving me an error saying that > was an invalid syntax. But the spur of the moment thought make me think to instead add a > b, a == b, and a < b, to next to the >>>compare. After one try i got this right away. This was a pretty easy program to make after I found out exactly what I had to do.
#2: def hypotenuse(a, b): """ >>> hypotenuse(3, 4) 5.0 >>> hypotenuse(12, 5) 13.0 >>> hypotenuse(7, 24) 25.0 >>> hypotenuse(9, 12) 15.0 """ if __name__== '__main__': import doctest doctest.testmod()
Which I then got an output of:

*****************************************************************************
File "hypotenuse.py", line 7, in __main__.hypotenuse
Failed example:
hypotenuse(7, 24)
Expected:
25.0
Got nothing
*****************************************************************************
File "hypotenuse.py", line 9, in __main__.hypotenuse
Failed example:
hypotenuse(9, 12)
Expected:
15.0
Got nothing
*****************************************************************************
1 items had failures:
4 of 4 in __ main__.hypotenuse
***Test Failed*** 4 failures.
#3: def slope(x1, y1, x2, y2): """ >>> slope(5, 3, 4, 2) 1.0 >>> slope(1, 2, 3, 2) 0.0 >>> slope(1, 2, 3, 3) 0.5 >>> slope(2, 4, 1, 2) 2.0 """ if __name__== '__main__': import doctest doctest.testmod()
 
With an end result of:
*****************************************************************************
File "slope.py", line 7, in __main__.slope
Failed example:
      slope(1, 2, 3, 3)
Expected:
      0.5
Got nothing
*****************************************************************************
File "slope.py", line 9, in __main__.slope
Failed example:
      slope(2, 4, 1, 2)
Expected:
       2.0
Got nothing
*****************************************************************************
1 items had failures:
    4 of      4 in __main__.slope
***Test Failed*** 4 failures.
ef intercept(x1, y1, x2, y2):
    """
      >>> intercept(1, 6, 3, 12)
      3.0
      >>> intercept(6, 1, 1, 6)
      7.0
      >>> intercept(4, 6, 12, 8)
      5.0
    """
if __name__== '__main__':
import doctest
doctest.testmod()
With a end result of:
*****************************************************************************
Ill have continue this tomorrow.  

Friday, February 4, 2011

Feb 4

Alright continuing onto using triple quotes, I learned that you can span the lines to where text appears on the next line, like so:
message = "This line will
...span several
....lines."""
And when you tell python to print message you get:
This line will
span several
lines.
This is because you can add as much words you want to this script. Then it will only give you the prompt of >>> when you finally add the last """ to the end of your lines.
#1:

Thursday, February 3, 2011

Feb 2-3

Today I am continuing onto chapter 5. Starting with fruitful functions. When working with python on the incremental development I've noticed that the distance program they tell you to program into python is basically the same as the real life formula when you code like:
def distance(x1, y1, x2, y2):
...     dx = x2 - x1
...     dy = y2 - y1
...     print "dx is", dx
...     print "dy is", dy
...     return 0.0
the d in this code is referring to distance then both the x and y used in this code stands for the x-axis and y-axis of a graph. This is a very interesting program to me and very easy to understand.
When working on getting the area of a circle i had some difficulty until I finally understand that what they was saying was to instead of start making a new program you just have to change the program you last did with distance to:
def area2(xc, yc, xp, yp)
...      radius = distance(xc, yc, xp, yp)
...      result = area(radius)
...      return result
so basically i had to fully change the distance program to make it fit the area of a cirlce program.
When it comes to boolean functions, they are pretty easy to understand when its basically a yes/or kind of question. At this point it told me to code a small script and when I finished it came up to have an error. What caused the error which took me forever to get why it was an error was instead of using a 0 in:
def is_divisible(x, y):
        return x % y = 0
I instead used an O on accident without even noticing.
When working with triple quotes, I've noticed that they work the same as just using one thing of quotes. The triple quotes still prints for example
"""Hello, world!""" when you can still use "Hello,World!" to get the same result.

Tuesday, February 1, 2011

Feb 1

#4: What I've noticed from the and and or expressions is that when using or and its only True and False then True will be the result but when you exchange True or False for a number or some other word then the result with ether be True or false or the new value you've plugged in.
#5: write a program.
#6: write a program.
#7: write a program.
#8: When I put in:
if 0:
        print "And now for something completely different..."
else:
        print "What's all this, then?"

and then i get an end result of:
What's all this, then?
from gasp import *

begin_graphics()

Box((20, 20), 100, 100)
Box((55, 20), 30, 50)
Box((40, 80), 20, 20)
Box((80, 80), 20, 20)
Line((20, 120), (70, 160))
Line((70, 160), (120, 120))

update_when('key_pressed')
end_graphics()
#10: um I haven't fully finished this one, I'm trying to understand what they meant by instead of using both the lines instead use 2 polygons. That confuses me cause when I went to the linked site openproject.net/thinkcs/python/english2e/app_b.html. Which this site describes what your suppost 2 apply when putting a polygon in, I just cant get the end result.
OK well I've now finally catch back up with chapter 5 so ill continue onto Chapter 5 tomorrow.