define/use variables

i want to add an simple interger “n”
e.g. each time a user press an “x”, n should count up.

def EventKeyX(self):

is not working, how do i have to do it? thx

There is no post-increment operator in Python; instead use

n += 1

You’ll probably want to store it in the class, so use self.n. Or, if it’s a global variable, put “global n” at the beginning of the function/method.

Here is the code:

import direct.directbase.DirectStart
from direct.showbase.DirectObject import DirectObject

class Program(DirectObject):
    def __init__(self):
    def inc(self):
        # may also written as:
        print self.n

That is the main idea

thx, its working! :slight_smile:

but how can i use combined ifs:

if self.x > -10 AND self.x < 10:

is not working!?

the syntax is “and”.

it could be :

if self.x > -10 and self.x < 10:

or even simpler :

if -10 < self.x < 10:

If you don’t understand these basic things, you’ll run into issues again and again. I really advise learning Python before attempting to program with Panda3D.

thx :slight_smile: + yes i’m new to the python syntax, but i found they have quite a good reference: :stuck_out_tongue: