[Tutor] Program Questions

Jeff Shannon jeff at ccvcorp.com
Thu Jul 15 20:20:28 CEST 2004


mandy bowen wrote:
> It tells me for the line:
> a = Account(1000.00)
> this constructor takes no arguments.
> It also says for all of the "print" lines following the class call in 
> MenuOption that "print" is invalid syntax.


>> > import time
>> >
>> > class Account:
>> >     def _init_(self, initial):
>> >         self.balance = initial
>> >         self.strdeposit = []
>> >         self.strwithdraw = []

Here's a tricky error that is often hard to spot, especially if you're 
learning from a book.  Magic methods in Python, like the init method, 
require *two* underscores on each side -- you have only a single 
underscore on each side.  You need to change that _init_() to be 
__init__().


 >> > class Menu:
 >> >     def DisplayMenu(self):
 >> >         print --------------------------------
 >> >         print -          Main Menu           -
 >> >         print --------------------------------


The problem here is that 'print' can take either variable names (in 
which case it prints the contents of the variable) or literal strings, 
which it prints as-is.  How does Python know which is which?  By how 
they're marked -- literal strings need to be surrounded by quotes. 
You can use either single quotes (') or double quotes (") -- that 
flexibility can be useful if you want printing a string that contains 
one of those characters.

If you go through each of your print statements and put quotes around 
the parts that are meant to be printed as-is, this should work.  For 
example, the bit I quoted above should become:

class Menu:
     def DisplayMenu(self):
         print "--------------------------------"
         print "-          Main Menu           -"
         print "--------------------------------"

Things get a little bit more complicated when you want to intermix 
literal strings with variables, as you do in the methods of your 
Account class.  For example, your getbalance() method should be
something like this:

      def getbalance(self):
          # print self.balance
          print time.strftime(%d %b %Y) + " $" + str(self.balance)

Here you have three parts (the time, the literal dollar sign, and the 
balance) which you're putting together into a single string (by using 
the + operator), and 'print' then acts on that single string.  (You 
also had a str() call around the whole thing, but that call is 
redundant -- the result of adding strings is already a string, so 
converting it into a *new* string accomplishes nothing.)

There's actually better ways to put variable values into a string. 
There's a nice-looking introduction into using the % operator for 
string formatting in "Dive Into Python" 
(http://diveintopython.org/native_data_types/formatting_strings.html)
which might be worth checking out.  There should also be something 
about this in whatever tutorial or book that you're using already -- 
check the contents/index for "string formatting".

Jeff Shannon
Technician/Programmer
Credit International



More information about the Tutor mailing list