您在這裡

Exercises

8 九月, 2015 - 10:43

Exercise 17.7. This exercise is a cautionary tale about one of the most common, and difficult to find, errors in Python. Write a definition for a class namedKangaroowith the following methods:

  1. An__init__method that initializes an attribute namedpouch_contentsto an empty list.
  2. A method namedput_in_pouchthat takes an object of any type and adds it topouch_contents.
  3. A __str__method that returns a string representation of the Kangaroo object and the contents of the pouch.

Test your code by creating twoKangarooobjects, assigning them to variables namedkangaandroo, and then addingrooto the contents ofkanga’spouch.

Download http: // thinkpython. com/ code/ BadKangaroo. py. It contains a solution to the previous problem with one big, nasty bug. Find and fix the bug.

If you get stuck, you can download http: // thinkpython. com/ code/ GoodKangaroo. py, which explains the problem and demonstrates a solution.

Exercise 17.8. Visual is a Python module that provides 3-D graphics. It is not always included in a Python installation, so you might have to install it from your software repository or, if it’s not there, from http: // vpython. org.

The following example creates a 3-D space that is 256 units wide, long and high, and sets the “center” to be the point (128, 128, 128). Then it draws a blue sphere.

from visual import *
 
scene.range = (256, 256, 256)
scene.center = (128, 128, 128)
 
color = (0.1, 0.1, 0.9) # mostly 
bluesphere(pos=scene.center, radius=128, color=color)

coloris anRGB tuple; that is, the elements are Red-Green-Blue levels between 0.0 and 1.0 (see http: // en. wikipedia. org/ wiki/ RGB_ color_ model).

If you run this code, you should see a window with a black background and a blue sphere. If you drag the middle button up and down, you can zoom in and out. You can also rotate the scene by dragging the right button, but with only one sphere in the world, it is hard to tell the difference.

The following loop creates a cube of spheres:

t = range(0, 256, 51)for x in t:for y in t:for z in t:pos = x, y, zsphere(pos=pos, radius=10, color=color)
  1. Put this code in a script and make sure it works for you.
  2. Modify the program so that each sphere in the cube has the color that corresponds to its position in RGB space. Notice that the coordinates are in the range 0–255, but the RGB tuples are in the range 0.0–1.0.
  3. Download http: // thinkpython. com/ code/ color_ list. py and use the function read_colors to generate a list of the available colors on your system, their names and RGB values. For each named color draw a sphere in the position that corresponds to its RGB values.

You can see my solution at http: // thinkpython. com/ code/ color_ space. py.