[Tutor] TypeError: not enough arguments for format string

Eric Abrahamsen eric at ericabrahamsen.net
Wed Jul 2 06:48:01 CEST 2008


On Jul 2, 2008, at 12:36 PM, Christopher Spears wrote:

> I'm working on problem 13-5 in Core Python Programming (2nd  
> Edition).  I am supposed to create point class.  Here is what I have  
> so far:
>
> #!/usr/bin/python
>
> class Point(object):
>    def __init__(self, x=0.0,y=0.0):
>        self.x = float(x)
> 	self.y = float(y)
> 	
>    def __repr__(self):
>        coord = (self.x,self.y)
> 	return coord
> 	
>    def __str__(self):
> 	point_str = "(%f,%f)" % self.x, self.y
> 	return point_str

Hi Christopher,
The problem's right above here – string formatting takes only one  
value. If you're trying format more than one string, you have to pass  
them in as a tuple. Try this:

point_str = "(%f,%f)" % (self.x, self.y)

>
> 	
>    def get_x(self):
>        return self.x
> 	
>    def get_y(self):
>        return self.y
> 	
> if __name__ == '__main__':
>    print "Creating a point"
>    x = raw_input("Enter a x value: ")
>    y = raw_input("Enter a y value: ")
>    p = Point(x,y)
>    print p
>
> I ran the script and got the following error message:
>
> Creating a point
> Enter a x value: 1
> Enter a y value: 2
> Traceback (most recent call last):
>  File "point.py", line 27, in ?
>    print p
>  File "point.py", line 13, in __str__
>    point_str = "(%f,%f)" % self.x, self.y
> TypeError: not enough arguments for format string
>
> Does anyone know what is wrong?  I'm sure it is something obvious,  
> but I can't see it.
>
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list