Python Development Course
Chapter
>
Level

Classes and Objects
Creating a class

Classes are much like templates you can create that hold blocks of code you can modify at will. You can use these templates to create individual objects of it. For example sat you create a class that outlines a chair, using the class you can create four different chair objects to compliment a table.

For this level, create a class outlining a grain stack, grab grains from the field and create an object of the class so you can use the build() function to create grain stacks in the field.

Guide

Objective

Grab all grains in the field and compile them into hay stacks using classes.

The grain in the field needs to be put together and compiled into stacks. In order to do this you will actually need to create objects. Classes are a way you can create objects, they are different from lists, variables and dictionaries in that these are self contained objects with their own properties.

class grain_stack: name = "Grain" number = 4 stack = grain_stack()

The code above is a class named grain_stack which holds the name of the objects inside and the quantity. Writing a name and setting the value as the class will create an individual object of this class, like this: stack = grain_stack() . Classes are very useful in that you can create objects of them with self contained information inside that you can modify at will.

Create a class for the grain_stack and an object of this class, collect all twelve (12) pieces of grain in the field (4 for each item). Walk to the X marks, use the build() function to put them together. This function can be used to put together objects and bring them into the field, like this: player.build(stack).

Code book