CS 1110 - Introduction to Programming

Starting Out with Python
Chapter 10, Classes and Object-Oriented Programming, part 3


This lesson returns to chapter 10, and elements from earlier chapters, to review concepts that are bothering some students. Objectives important to this lesson:

  1. Common problems
  2. Algorithm workbench
Chapter 10, part 3

Common Problems

There are a lot of things that can go wrong in a Python program, and our lessons have introduced us to several of them. In the announcement I sent out this week, I listed a few things to troubleshoot when you are having problems:

  • A common source of errors in programs can be mistyping.
    To address that issue, make sure you spell the name of each object, method, attribute, and structure the same way everywhere that name is used.
  • While you are at it, watch the indentation for every line. Each sub-block needs more indentation than the parent block has. Blocks that are executed from a conditional phrase are sub-blocks.
  • To apply indentation, use either tabs or spaces, but don't use both. That can confuse the interpreter.
  • Stop using the current indentation when you end a block, but continue using it if the next line is part of the current block.
  • Every opening parenthesis requires a closing parenthesis. Figure out where it goes and use it.
  • Another error can be caused by an error in a file copied from one device to another. If you are getting errors when nothing looks wrong, as I am seeing in chapter 10 programs, try deleting and retyping the first lines of __init__ modules. The spaces, the tabs, or the underscores may be mangled. This corrected the errors that said "object takes no arguments" in some of the files I received.

Take a look at this short article about common user errors in Python programs: http://www.onlamp.com/pub/a/python/2004/02/05/learn_python.html. The author mentions several more errors that are commonly seen, starting on the second half of the first page. The second page gets into concepts we have not hit yet, as well as some we have.

  • Use colons where they are required: at the end of a conditional statement, at the end of the header of a loop (which is kind of like a conditional), and at the end of the first lines of function and class definitions. And remember that the next line after that line begins a new indentation scheme, because it is the start of a block, even if that block is one line long.
  • Variables cannot be used until they have been assigned values. Initialize your variables.
  • Imports should be done at the top of a file, because the functions and other goodies in the imported file need to be read by the interpreter before you call any of them.
Algorithm Workbench

Let's take a look at the Algorithm workbench in chapter 10. It starts out simply. Problem 1 says we have a variable named my.car, and it has a method named go. This should tells us that this variable is actually an object, which it must be if it has a method in it. How do we access that method?

The recipe for that notation is object_name.method_name(). In this case that means my.car.go().

The second problem takes us to the next level. It tells us that we are going to write a class definition. The class is called Book. That means it will start like this:

class Book:

The problem says it will have attributes that hold the title, author's name, and publisher's name. This invites us to create those attributes and assign values to them when an instance is created. That works with the next requirements, which are to create an __init__ method, a __str__ method, and accessor and mutator methods for each attribute. So we will add parameters to the __init__ method for self, title, author, and publisher, then add the attributes in the ___init__ method, and add the other methods that are required.

class Book:
    def __init__
(self, title, author, publisher):
       self.__title = title
       self.__author = author
       self.__publisher = publisher

    def get_title(self):
       return self.__title

    def get_author(self):
       return self.__author

    def get_publisher(self):
       return self.__publisher

    def set_title(self, title):
self.__title = title

    def set_author(self, author):
       self.__author = author

    def set_publisher(self, publisher):
       self.__publisher = publisher

    def __str__(self):
       return "Title: " + self.__title + "\nAuthor: " + self.__author + "\nPublisher: " + self.__publisher

This is the standard layout for a class: an __init__ method, a __str__ method, and both an accessor and a mutator method for each attribute.

The third problem in this section takes us into the realm of  story problems. Our customer is a bank. The bank has three kinds of accounts that customer may use, savings, checking, and money market accounts. Each must allow deposits and withdrawals that affect the account's balance. It is debatable whether we need one kind of class or three. With the information above, one could do as well as three. Let's talk about this in class.


For this assignment, return to chapter 8 and do problems 8 and 10.
8. Write a program with a function that accepts a string as an argument and returns a copy of the string with the first character of each sentence capitalized. For instance, if the argument is “hello. my name is Joe. what is your name?” the function should return the string “Hello. My name is Joe. What is your name?” The program should let the user enter a string and then pass it to the function. The modified string should be displayed.

10. Write a program that lets the user enter a string and displays the character that appears most frequently in the string.