simple question

Bjorn Pettersen BPettersen at NAREX.com
Thu Aug 16 22:57:29 CEST 2001


> From: Bignose3 [mailto:k98jh01 at kzoo.edu]
> 
> I am a student starting to learn python coming from a C++ background.
> I am writing a simple simulation program were I am trying to assign
> each "swimmer" an ID.  Unfortunatley the way I would do it in C++
> doesnt work for python.  Can someone guide me in the right direction.
> 
> Here is the code that doesnt work, see what I am trying to do:
> 
> nextID= 0
> class swimmer:
>     def __init__(self, xpos, ypos):
+         global nextID
>         self.ID = nextID
>         nextID= nextID + 1
>         self.xpos= xpos
>         self.ypos= ypos
>     def xcord(self):
>         print self.xpos
>     def ycord(self):
>         return self.ypos
>     def swimmerID(self):
>         return self.ID
> 
> An explanation would be great to.  Not a complex program but you have
> to start somewhere:)

If you're going to assign to global variables, you'll need to let Python
know that you're talking about the global and not trying to create a new
local variable with the same name.

-- bjorn




More information about the Python-list mailing list