<div class="gmail_quote">On 21 December 2011 19:12, Dean Richardson P.Eng <span dir="ltr"><<a href="mailto:dean@red-spanner.com" target="_blank">dean@red-spanner.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi All,<br>I'm a newbie just learning Python, using a couple of books to learn the language. (Books: "Visual Quickstart Guide - Python, 2nd Ed", "Practical Programming - An Intro to Computer Science using Python"). I'm just now learning OOP material as presented in both books -- I'm new to this approach -- the last formal language I learned was Fortran77 -- :o) I am running Python 3.2.1 on Mac OS/X 10.6.8. <br>
<br>My problem stems from a simple example in the Visual Quickstart book. The code is:<br>----------------------------<br>#person.py<br>class Person:<br> """Class to represent a person"""<br>
def __init__(self):<br> <a href="http://self.name" target="_blank">self.name</a>=' '<br> self.age=0<br> def display(self):<br> print("Person('%s', age)" %<br> (<a href="http://self.name" target="_blank">self.name</a>, self.age))<br>
</blockquote><div><br></div><div>The "% (<a href="http://self.name">self.name</a>, self.age)" means that two arguments are given in. You only have one "%s" in the string (untested, so I may be completely off).</div>
<div><br></div><div>
Additionally, use str.format now:</div>
<div>"Person({}, {})".format(<a href="http://self.name" target="_blank">self.name</a>, self.age)</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-------------------------<br> When I run this, the shell presents thus:<br>>>> ================================ RESTART ================================<br>>>> <br>>>> p=Person()<br>>>> <a href="http://p.name" target="_blank">p.name</a>='Bob'<br>
>>> p.age=24<br>>>> p.display()<br>Traceback (most recent call last):<br> File "<pyshell#33>", line 1, in <module><br> p.display()<br> File "/Volumes/dean_richardson/GoFlex Home Personal/Dean's Code/Python3.x/Person.py", line 9, in display<br>
(<a href="http://self.name" target="_blank">self.name</a>, self.age))<br>TypeError: not all arguments converted during string formatting<br>>>> <br>---------------<br>I'm sure this is something simple, but I can't see it. Any help appreciated!<br>
</blockquote></div><br>