I want to reverse a sensor (like pressing the invert button) however using sensor.negative doesn’t seem to work even with negative triggering on the sensor. The console returns that the keyboard sensor does not have the attribute ‘negative’.

How can I invert the input of a sensor without joining a second sensor to the python script?

Thanks :smiley:

When a controller receives a “negative pulse”, the script runs, but sensor.positive evaluates to False, so:

sensor_negative = not sensor.positive


sensor.invert = True

will essentially press the invert button.

Awesome, thanks guys :smiley:

also, to add:
sensor.positive is a boolean, so you can check it for a negative pulse with “sensor.positive == False”

It makes not much sense to check a boolean value to get another boolean value. “not sensor.positive” is the way to go.

I personally find

if sensor.positive == False

easier to read, simply because my computer-science-confused brain doesn’t like having if statements simply with a value in them. I also use:

if sensor.positive == True

For the same reason, just seems more, well, consistent to me.

I agree, that looks better.

It is similar to write x=y1 + n0. It is not wrong, but it adds additional operations without effecting the result.

You can read about the “if”-statement in PEP#008.