[Tutor] TypeError

Kent Johnson kent37 at tds.net
Sun Feb 3 22:43:43 CET 2008


Seon Kang wrote:
> Why does it not specify the type?

Actually, it is specifying the type of the bad argument, which is itself 
'type'. Confusing, I know. Try this:
 >>> 1+int
------------------------------------------------------------
Traceback (most recent call last):
   File "<ipython console>", line 1, in <module>
<type 'exceptions.TypeError'>: unsupported operand type(s) for +: 'int' 
and 'type'

The type of 1 is 'int' and the type of int is 'type'. It is telling you 
that you are trying to add an integer to a type object.

> here is the entire section of the module that i imported that's giving 
> me the trouble. Why is it giving the TypeError?

It would really help to see *your* code and the *complete* traceback. 
Can you figure out where _dx and _dy are coming from? They don't seem to 
be assigned in the code you show here.

Also please use Reply All to reply to the list.

Kent

>  
>  
> 
> class Sprite(object):
>     def __init__(self, image, angle=0,
>                  x=0, y=0,
>                  top=None, bottom=None, left=None, right=None,
>                  dx=0, dy=0,
>                  interval=1, is_collideable=True):
>        
>         if not Screen.initialized:
>             raise GamesError, "Screen object must be intialized before 
> any Sprite object"
>  
>         self._surface = image
>         self._orig_surface = image    # Surface before any rotation
>         self._rect = self._surface.get_rect()
> 
>         self.position = (x, y)
> 
>         if top != None:
>             self.top = top
>         if bottom != None:
>             self.bottom = bottom
>         if left != None:
>             self.left = left
>         if right != None:
>             self.right = right
> 
>         self.velocity = (dx, dy)
> 
>         self._angle = angle % 360
>         if self._angle != 0:
>             self._rotate()
> 
>         self.is_collideable = is_collideable
> 
>         self._interval = interval
>         self._tickable = 1
>         self._next = 0
> 
>         self._gone = 0
> 
>     def __del__(self):
>         if screen and not self._gone:
>             self.destroy()
> 
>     def _draw(self):
>         """
>         Draw object on screen by blitting the image onto the screen.
>         """
>         screen.blit_and_dirty(self._surface, self._rect)
> 
>     def _erase(self):
>         """
>         Erase object from screen by blitting the background over where 
>         it was.
>         """
>         screen.blit_background(self._rect)
>  
>     def _replace(self, new_surface):
>         x, y = self.position
>         self._surface = new_surface
>         self._rect = self._surface.get_rect()
>         self.position = (x, y)
> 
>     def _rotate(self):
>         self._replace(pygame.transform.rotate(self._orig_surface, 
> -self._angle))
> 
>     def _tick(self):
>         self._next = self._next + 1
>         if self._next >= self._interval:
>             self._next = 0 
>             self.tick()
>         if self._dx or self._dy:
>             self.position = ( (self._x + self._dx), (self._y + self._dy) )
>         self.update()
> 
>     def start (self):
>         self._tickable = 1
>         self._next = 0
> 
>     def stop (self):
>         self._tickable = 0
> 
>     def update(self):
>         pass
> 
>     def tick(self):
>         pass
>  
>     def overlaps(self, other):
>         if not self.is_collideable or not other.is_collideable:
>             return False
>         else:
>             return self._rect.colliderect(other._rect)
>     
>     def elevate(self, above=None):
>         """
>         Elevate an object to the top of the stack, or above the specified
>         object.
>         """
>         screen._elevate(self, above)
>     
>     def lower(self, below=None):
>         """
>         Lower an object to the bottom of the stack, or below the specified
>         object.
>         """
>         screen._lower(self, below)
> 
>     def destroy(self):
>         """
>         Erase object from screen and remove it from the list of objects
>         maintained by games module.
>         """
>         self._erase()
>         screen.remove(self)
>         self._gone = 1
> 
> 
> 
>  
> On 2/3/08, *Kent Johnson* <kent37 at tds.net <mailto:kent37 at tds.net>> wrote:
> 
>     Seon Kang wrote:
>      > When i tried to run my program, i was given this message
>      > (this is the message in part)
>      >
>      > file "C:Python25\lib\site-packages\livewires\games.py", line 503,
>     in_tick
>      >     self.position = ((self._x + self._dx), (self._y + self._dy))
>      > TypeError: unsopported opernad type(s) for +: 'int' and type'
>      >
>      > What is the nature of my problem?
> 
>     It seems that either self._dx or self._dy is not an integer.
> 
>      > But more specifically, what is the 'type' it's referring to?
> 
>     A type is the class of a value. For example, int, str and float are all
>     types.
> 
>     If you show us the full traceback and some of your code we can be more
>     helpful.
> 
>     Kent
> 
> 



More information about the Tutor mailing list