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):
    n++

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):
        self.n=0
        self.accept("x",self.inc)
        
    def inc(self):
        self.n+=1
        # may also written as:
        #self.n=self.n+1
        print self.n
w=Program()
run()

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: wiki.python.org/moin/BeginnersGuide/Programmers :stuck_out_tongue: