Thursday, January 29, 2009

Chapter 4 -- Page 128 -- Problem 4.2g

In Python3.0, the sort method will not work across different data types. Performing sort on the list in this problem will cause an error.

>>> alist
[7, 9, 'a', 'cat', False]
>>> alist.sort()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unorderable types: str() < int()

Chapter 2 -- Page 78 -- Listing 2.6

The variable numdarts in line 5 and line 23 should be numDarts (camelcase).

Chapter 2 -- Page 75 -- Problem 2.38

This problem asks you to create a boolean function called inCircle that returns True if the point is in the circle, False otherwise. Unfortunately, there is a local variable in the montePi function already called inCircle.  For clarity, use a different name for the boolean function...perhaps isInCircle or pointInCircle.

Wednesday, January 28, 2009

Chapter 13 -- Etch

Well here it is, the first bug report I've received from a reader. Thanks to Gregor Lingl (The same guy who wrote the excellent xturtle module that we based cTurtle on).

Listing 13.4 is wrong. Listing 13.4 provides a class that inherits from Turtle and provides simple onKey callbacks for turning left, right, forward and backward. These callbacks should turn a default amount or go back or forward a default number of spaces.

To avoid a crazy recursion the callbacks were named left5 and right5 in the working version of the code, but somehow the 5 got left off in the published listing.

Here's a complete, tested and working version of listing 13.4


import sys
import os
from cTurtle import Turtle, mainloop

class Etch(Turtle):
defaultDist = 5
def __init__(self):
super(Etch, self).__init__()
self.color('blue')
self.pensize(2)
self.speed(0)
self.distance = 5
self.turn = 5
self.onKey(self.forward,"Up")
self.onKey(self.bkwd,"Down")
self.onKey(self.left5,"Left")
self.onKey(self.right5,"Right")
self.onKey(self.quit,"q")
self.listen()
self.main()

def forward(self,distance=defaultDist):
super(Etch,self).forward(distance)

def bkwd(self):
self.backward(self.distance)

def left5(self):
self.left(self.turn)

def right5(self):
self.right(self.turn)

def quit(self):
sys.exit()

def main(self):
mainloop()

if __name__ == '__main__':
etch = Etch()