Skip to Content
A Concise Introduction to Programming in Python
book

A Concise Introduction to Programming in Python

by Mark J. Johnson
December 2011
Beginner
217 pages
8h
English
Chapman and Hall/CRC
Content preview from A Concise Introduction to Programming in Python
Chapter 24
Cooperating Classes
Classes are rarely used in isolation. Their power comes from interactions be-
tween objects, and choosing useful classes is the art of object-oriented design.
The dice game Chuck-a-Luck provides a good example of a relatively simple
game with enough complexity to warrant more than one class. Three dice are
rolled, and the player needs to guess one of the numbers that will appear. If
the number appears (either 1, 2, or 3 times), the player wins that many units;
otherwise, the player loses one unit.
Listing 24.1: Chuck-a-Luck
1 # chuck.py
2
3 from dice import Dice
4
5 class ChuckALuck:
6 def __init__(self):
7 self.dice = Dice(3) ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python Standard Library

Python Standard Library

Fredrik Lundh
Numerical Computing with Python

Numerical Computing with Python

Pratap Dangeti, Allen Yu, Claire Chung, Aldrin Yim

Publisher Resources

ISBN: 9781439896952