Skip to main content
Engineering LibreTexts

8.13: Exercises

  • Page ID
  • Exercise \(\PageIndex{1}\)

    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 named Kangaroo with the following methods:

    1. An __init__ method that initializes an attribute named pouch_contents to an empty list.
    2. A method named put_in_pouch that takes an object of any type and adds it to pouch_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 two Kangaroo objects, assigning them to variables named kanga and roo, and then adding roo to the contents of kanga’s pouch.

    Download 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, which explains the problem and demonstrates a solution.

    Exercise \(\PageIndex{2}\)

    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

    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) = (128, 128, 128)
    color = (0.1, 0.1, 0.9)          # mostly blue
    sphere(, radius=128, color=color)

    color is an RGB tuple; that is, the elements are Red-Green-Blue levels between 0.0 and 1.0 (see

    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, z
                sphere(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 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.