Friday, October 30, 2009

Chapter 5 Exercises

Chapter 5 Exercises at http://docs.google.com/View?id=d5d8bxk_67htqczwck
  • As I get further on in the book, the more fun the exercises are. In Chapter 5 to complete the exercises you really need to think and problem solve in order to make sure the programs pass the doctests. These problems all involved a systematic testing and failing process in order to achieve the solution. I expected to fail, but in the failure I could pinpoint the problem and correct it. These problems also required the use of the return function and I learned how it could be used with boolean functions so that the use of if and else statements don't need to be used.
  • the slope, intercept, Fahrenheit to Celsius and vice-versa functions all require the use of equations and formulas to complete the exercises.
  • The intercept problem was especially fun because it involved relating the point-slope and slope-intercept forms in order to get the equation for the y-intercept.
  • The Fahrenheit-Celsius problems were also fun because they involved the use of the int and round functions.

Wednesday, October 21, 2009

Chapter 4 Exercises

Chapter 4 Exercises are published at http://docs.google.com/View?id=d5d8bxk_61f6tqkndq

  • The third problem seemed a bit pointless at first, but it was good to show how logical operators work.
  • Problem number 4 involved evaluation of boolean expressions and logical operators to complete and after some trouble I finally completed the rules of "and" and "or" operators and found the problem to be one of the best for reinforcing the chapter through the process of actually doing.
  • Problem 6 was redundant in that if you could do the first part the second part was no different and there seemed no point in doing them both.
  • Number 8 was also a good example of how conditional statements work, another good look at boolean expressions, and also a good look at the flow of execution.
  • Number 9 was a good problem on learning how to use GASP and the process of translating the house around the screen by parameterizing the original draw_house() definition took some time to get.



Monday, October 5, 2009

Chapter 3 Assignments

3.8.1

def new_line():

print


def three_lines():

new_line()

new_line()

new_line()


def nine_lines():

three_lines()

three_lines()

three_lines()


def clear_screen():

nine_lines()

nine_lines()

three_lines()

three_lines()

new_line()

clear_screen()

3.8.2

clear_screen()


def new_line():

print


def three_lines():

new_line()

new_line()

new_line()


def nine_lines():

three_lines()

three_lines()

three_lines()


def clear_screen():

nine_lines()

nine_lines()

three_lines()

three_lines()

new_line()


Traceback (most recent call last):

File "tryme3_2.py", line 3, in <module>

clear_screen()

NameError: name 'clear_screen' is not defined

  • Function calls must come after a function definition. If not, the function will be undefined.

3.8.3
def three_lines():

new_line()

new_line()

new_line()


def new_line():

print


def nine_lines():

three_lines()

three_lines()

three_lines()

def clear_screen():

nine_lines()

nine_lines()

three_lines()

three_lines()

new_line()


clear_screen()

  • This still works because the function definitions all come before the call that incorporates all the definitions regardless of order.

def three_lines():
new_line()
new_line()
new_line()

three_lines()

def new_line():
print

Traceback (most recent call last):
File "/home/louis/bin/exercise3.8.3", line 8, in <module>
three_lines()
File "/home/louis/bin/exercise3.8.3", line 4, in three_lines
new_line()
NameError: global name 'new_line' is not defined

  • This does not work because the function three_lines() requires the function new_line() which is not defined until after the function call of three_lines

3.8.4
def cat_n_times(s, n):
print s*n

>>> from import_test import*
>>> cat_n_times("Cats have nine lives. ", 9)
Cats have nine lives. Cats have nine lives. Cats have nine lives. Cats have nine lives. Cats have nine lives. Cats have nine lives. Cats have nine lives. Cats have nine lives. Cats have nine lives.